Skip to content

Instantly share code, notes, and snippets.

@patrickkeller
Created December 10, 2013 13:36
Show Gist options
  • Save patrickkeller/7890671 to your computer and use it in GitHub Desktop.
Save patrickkeller/7890671 to your computer and use it in GitHub Desktop.
node.js init script
#/bin/sh
NODE_EXEC=/usr/local/bin/node
NODE_ENV="production"
NODE_APP='app.js'
APP_DIR='/var/www/example.com';
PID_FILE=$APP_DIR/pid/app.pid
LOG_FILE=$APP_DIR/log/app.log
CONFIG_DIR=$APP_DIR/config
###############
# REDHAT chkconfig header
# chkconfig: - 58 74
# description: node-app is the script for starting a node app on boot.
### BEGIN INIT INFO
# Provides: node
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop node
# Description: Node process for app
### END INIT INFO
case "$1" in
start)
if [ -f $PID_FILE ]
then
echo "$PID_FILE exists, process is already running or crashed"
else
echo "Starting node app..."
NODE_ENV=$NODE_ENV NODE_CONFIG_DIR=$CONFIG_DIR $NODE_EXEC $APP_DIR/$NODE_APP 1>$LOG_FILE 2>&1 &
echo $! > $PID_FILE;
fi
;;
stop)
if [ ! -f $PID_FILE ]
then
echo "$PID_FILE does not exist, process is not running"
else
echo "Stopping $APP_DIR/$NODE_APP ..."
echo "Killing `cat $PID_FILE`"
kill `cat $PID_FILE`;
rm $PID_FILE;
echo "Node stopped"
fi
;;
restart)
if [ ! -f $PID_FILE ]
then
echo "$PID_FILE does not exist, process is not running"
else
echo "Restarting $APP_DIR/$NODE_APP ..."
echo "Killing `cat $PID_FILE`"
kill `cat $PID_FILE`;
rm $PID_FILE;
NODE_ENV=$NODE_ENV NODE_CONFIG_DIR=$CONFIG_DIR $NODE_EXEC $APP_DIR/$NODE_APP 1>$LOG_FILE 2>&1 &
echo $! > $PID_FILE;
echo "Node restarted"
fi
;;
status)
if [ -f $PID_FILE ]
then
PID=`cat $PID_FILE`
if [ -z "`ps -ef | grep $PID | grep -v grep`" ]
then
echo "Node app stopped but pid file exists"
else
echo "Node app running with pid $PID"
fi
else
echo "Node app stopped"
fi
;;
*)
echo "Usage: /etc/init.d/node-app {start|stop|restart|status}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment