Skip to content

Instantly share code, notes, and snippets.

@komeda-shinji
Created March 2, 2015 06:55
Show Gist options
  • Save komeda-shinji/3ca66325952b4e64e26d to your computer and use it in GitHub Desktop.
Save komeda-shinji/3ca66325952b4e64e26d to your computer and use it in GitHub Desktop.
rrdcached.init
#! /bin/bash
#
# rrdcached - start and stop the RRDtool data caching daemon
# http://oss.oetiker.ch/rrdtool/
#
# Based on the collectd init script.
#
# Copyright (C) 2005-2006 Florian Forster <[email protected]>
# Copyright (C) 2006-2009 Sebastian Harl <[email protected]>
#
### BEGIN INIT INFO
# Provides: rrdcached
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start the RRDtool data caching daemon
### END INIT INFO
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DISABLE=0
DESC="RRDtool data caching daemon"
NAME=rrdcached
DAEMON=/usr/bin/rrdcached
RRDCACHEDIR="/var/opt/rrd/rrdcached"
RRDCACHEJOURNAL="${RRDCACHEDIR}/journal"
RRDCACHEDB="${RRDCACHEDIR}/db"
RRDCACHESOCKFILE="${RRDCACHEDIR}/rrdcached.sock"
RUN_USER='rrdcached'
RUN_GROUP='monitor'
OPTS="-m 660 -l unix:${RRDCACHESOCKFILE}"
OPTS="$OPTS -j ${RRDCACHEJOURNAL}/ -F"
OPTS="$OPTS -b ${RRDCACHEDB}/"
#OPTS="$OPTS -B"
OPTS="$OPTS -w 1800 -z 1800 -f 3600"
RUNAS="-c ${RUN_USER}:${RUN_GROUP}"
#PIDFILE=/var/run/rrdcached.pid
PIDFILE=${RRDCACHEDIR}/rrdcached.pid
MAXWAIT=30
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
if [ -r /etc/default/$NAME ]; then
. /etc/default/$NAME
fi
if test "$DISABLE" != 0 -a "$1" == "start"; then
echo "$NAME has been disabled - see /etc/default/$NAME."
exit 0
fi
if test "$ENABLE_COREFILES" == 1; then
ulimit -c unlimited
fi
d_start() {
if test "$DISABLE" != 0; then
# we get here during restart
echo -n " - disabled by /etc/default/$NAME"
return 0
fi
[ -d "${RRDCACHEDIR}" ] || mkdir -m 775 "${RRDCACHEDIR}"
[ -d "${RRDCACHEJOURNAL}" ] || mkdir -m 775 "${RRDCACHEJOURNAL}"
[ -d "${RRDCACHEDB}" ] || mkdir -m 775 "${RRDCACHEDB}"
[ -d "${RRDCACHEDIR}" ] && chown -R "${RUN_USER}" "${RRDCACHEDIR}"
start-stop-daemon --start --quiet --oknodo --pidfile "$PIDFILE" \
--exec $DAEMON $RUNAS -- $OPTS -p "$PIDFILE"
}
still_running_warning="
WARNING: $NAME might still be running.
In large setups it might take some time to write all pending data to
the disk. You can adjust the waiting time in /etc/default/$NAME."
d_stop() {
PID=$( cat "$PIDFILE" 2> /dev/null ) || true
start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE"
sleep 1
if test -n "$PID" && kill -0 $PID 2> /dev/null; then
i=0
while kill -0 $PID 2> /dev/null; do
i=$(( $i + 2 ))
echo -n " ."
if test $i -gt $MAXWAIT; then
echo "$still_running_warning" >&2
return 1
fi
sleep 2
done
return 0
fi
}
d_status() {
PID=$( cat "$PIDFILE" 2> /dev/null ) || true
if test -n "$PID" && kill -0 $PID 2> /dev/null; then
echo "$NAME ($PID) is running."
exit 0
else
PID=$( pidof $NAME ) || true
if test -n "$PID"; then
echo "$NAME ($PID) is running."
exit 0
else
echo "$NAME is stopped."
fi
fi
exit 1
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
status)
d_status
;;
restart|force-reload)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
# vim: syntax=sh noexpandtab sw=4 ts=4 :
@komeda-shinji
Copy link
Author

RHELの場合

--- rrdcached.init  2015-03-02 22:03:38.548161387 +0900
+++ /etc/init.d/rrdcached   2015-03-02 21:59:54.992879625 +0900
@@ -20,15 +20,19 @@
 # Short-Description: start the RRDtool data caching daemon
 ### END INIT INFO

+if [ -r /etc/init.d/functions ]; then
+   . /etc/init.d/functions
+fi
+
 set -e

-PATH=/sbin:/bin:/usr/sbin:/usr/bin
+PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

 DISABLE=0

 DESC="RRDtool data caching daemon"
 NAME=rrdcached
-DAEMON=/usr/bin/rrdcached
+DAEMON=/usr/local/bin/rrdcached

 RRDCACHEDIR="/var/opt/rrd/rrdcached"
 RRDCACHEJOURNAL="${RRDCACHEDIR}/journal"
@@ -44,7 +48,8 @@
 #OPTS="$OPTS -B"
 OPTS="$OPTS -w 1800 -z 1800 -f 3600"

-RUNAS="-c ${RUN_USER}:${RUN_GROUP}"
+#RUNAS="-c ${RUN_USER}:${RUN_GROUP}"
+RUNAS="--user ${RUN_USER}"

 #PIDFILE=/var/run/rrdcached.pid
 PIDFILE=${RRDCACHEDIR}/rrdcached.pid
@@ -74,13 +79,12 @@
        return 0
    fi

-   [ -d "${RRDCACHEDIR}" ] || mkdir -m 775 "${RRDCACHEDIR}"
+   [ -d "${RRDCACHEDIR}" ] || mkdir -m 775 -p "${RRDCACHEDIR}"
    [ -d "${RRDCACHEJOURNAL}" ] || mkdir -m 775 "${RRDCACHEJOURNAL}"
    [ -d "${RRDCACHEDB}" ] || mkdir -m 775 "${RRDCACHEDB}"
    [ -d "${RRDCACHEDIR}" ] && chown -R "${RUN_USER}" "${RRDCACHEDIR}"

-   start-stop-daemon --start --quiet --oknodo --pidfile "$PIDFILE" \
-       --exec $DAEMON $RUNAS -- $OPTS -p "$PIDFILE"
+   daemon $RUNAS $DAEMON $OPTS -p "$PIDFILE"
 }

 still_running_warning="
@@ -91,7 +95,7 @@
 d_stop() {
    PID=$( cat "$PIDFILE" 2> /dev/null ) || true

-   start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE"
+   killproc -p "$PIDFILE" rrdcached

    sleep 1
    if test -n "$PID" && kill -0 $PID 2> /dev/null; then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment