Skip to content

Instantly share code, notes, and snippets.

@premist
Forked from bmeck/service
Created April 20, 2012 02:34
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 premist/2425424 to your computer and use it in GitHub Desktop.
Save premist/2425424 to your computer and use it in GitHub Desktop.
init.d script
#!/bin/bash
#
# initd-example Node init.d
#
# chkconfig: 345 80 20
# description: Node init.d example
# processname: node
# pidfile: /var/run/initd-example.pid
# logfile: /var/log/initd-example.log
#
# Source function library.
. /lib/lsb/init-functions
path=$path:/user/local/bin
NAME=forever-services
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever_dir=/usr/bin/forever # Forever root directory.
node=node
forever=`which forever`
awk=awk
sed=sed
echo `env`
start() {
echo "Starting $NAME node instance: "
if [ "$id" = "" ]; then
# Create the log and pid files, making sure that the target use has access to them
touch $logfile
chown $USER $logfile
touch $pidfile
chown $USER $pidfile
# Launch the application
start_daemon -p $pidfile $forever service-start -p $forever_dir
else
echo "Instance already running"
fi
RETVAL=$?
}
restart() {
echo -n "Restarting $NAME node instance : "
if [ "$id" != "" ]; then
$forever service-restart -p $forever_dir
RETVAL=$?
else
start
fi
}
stop() {
echo -n "Shutting down $NAME node instance : "
if [ "$id" != "" ]; then
$forever service-stop -p $forever_dir
else
echo "Instance is not running";
fi
RETVAL=$?
}
getForeverId() {
local pid=$(pidofproc -p $pidfile)
$forever list -p $forever_dir | $sed -e 's/\x1b\[[0-9; ]*m//g' | $awk "\$4 == \"$pid]\" { gsub(/[\[\]]/, \"\", \$1); print \$1; }"
}
id=$(getForeverId)
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile}
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment