Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mplscorwin
Forked from garlandkr/statsd_init.sh
Last active January 3, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mplscorwin/8439751 to your computer and use it in GitHub Desktop.
Save mplscorwin/8439751 to your computer and use it in GitHub Desktop.
this version (which I use on CentOS as /etc/init.d/statsd) mimics more closely the "normal" sysV service control script behavior.
#!/bin/sh
### BEGIN INIT INFO
# Provides: statsd
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: control statsd
# Description: start/stop/restart statsd using nodejs forever
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
exec="/opt/statsd/stats.js"
exec_ext=".js"
pidfile="/var/run/statsd.pid"
CONFIG="/opt/statsd/local.js"
LOG_DIR="/var/log/statsd"
node="/usr/local/bin/node"
forever="/usr/local/bin/forever"
forever_opts="--minUptime 1000 --spinSleepTime 1000 -al $LOG_DIR/forever.log -ao $LOG_DIR/statsd.log -ae $LOG_DIR/statsd.log"
start_command="$forever start $forever_opts $exec $CONFIG"
[ -e /etc/sysconfig/statsd ] && . /etc/sysconfig/statsd
lockfile=/var/lock/subsys/statsd
my_basename=`basename $exec $exec_ext`
start() {
[ -f $CONFIG ] || exit 6
[ -e $exec ] || exit 5
echo -n $"Starting $my_basename: "
# STDOUT redirection to suppress "forever process FOO" message
daemon "$start_command >/dev/null"
retval=$?
echo
if [ $retval -eq 0 ] ; then
touch $lockfile
# fancy footwork to get the proper pid into the pidfile note
# that we we actually put in here is the pid of forever's
# monitor as the script pid when/if forever restarts statsd
$forever list --plain | /bin/grep $exec | /bin/awk '{ print $7 }' > $pidfile
fi
return $retval
}
stop() {
echo -n $"Stopping $my_basename: "
#killproc -p $pidfile $my_basename
$forever stop $exec >/dev/null 2>&1
retval=$?
if [ "$retval" -eq 0 ] ; then
echo_success
else
echo_failure
fi
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
rh_status() {
#status -p $pidfile $my_basename
#status -l $lockfile $my_basename
status -p $pidfile -l $lockfile $exec
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && ( echo "$my_basename already running" ; exit 0 )
$1
;;
stop)
rh_status_q || ( echo "$my_basename not running" ; exit 0 )
$1
;;
restart|reload)
#rh_status_q && stop
#rh_status_q || start
#$1
restart
;;
# reload)
# $1
# ;;
status)
rh_status
;;
# condrestart|try-restart)
# rh_status_q || exit 0
# restart
# ;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment