Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Last active June 15, 2019 03:31
Show Gist options
  • Save bodokaiser/cbae884f43041cc333bc to your computer and use it in GitHub Desktop.
Save bodokaiser/cbae884f43041cc333bc to your computer and use it in GitHub Desktop.
supervisor (node.js) init script - works with nvm and custom paths :)
#!/bin/sh
#
# chkconfig: 35 99 99
# description: node.js /home/bodokaiser/tmp/<name>
#
. /etc/rc.d/init.d/functions
# application name
NAME="myapp"
# application user
USER="bodokaiser"
# environment variable
ENV="NODE_ENV=production"
# path to executable (we use supervisor for code reload)
EXEC='$(which supervisor)'
# supervisor flags:wq
FLAGS="--harmony -e js,json,jade -i src -n error"
# path to nodejs entry script
SCRIPT="~/tmp/$NAME/lib"
# path to log file
LOGFILE="/home/$USER/tmp/$NAME.log"
# path to lock file
PIDFILE="/var/run/node-$NAME.pid"
start()
{
if [ ! -f $PIDFILE ]
then
echo -e $"Starting $NAME: "
PID=`runuser -l $USER -c "$ENV $EXEC $FLAGS $SCRIPT" >> $LOGFILE 2>&1 & echo $!`
if [ ! -z $PID ]
then
echo $PID > $PIDFILE
echo_success
else
echo_failure
fi
return $?
else
echo -e $"Already running $NAME."
echo_failure
return 1
fi
}
stop()
{
if [ -f $PIDFILE ]
then
echo -e $"Stopping $NAME: "
PID=`cat $PIDFILE`
kill -TERM $PID
if [ $? ]
then
echo_success
rm -f $PIDFILE
else
echo_failure
fi
return $?
else
echo -e $"Not started $NAME."
echo_failure
return 1
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment