Skip to content

Instantly share code, notes, and snippets.

@rudylacrete
Created July 14, 2014 09:20
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 rudylacrete/ab057f662763007a930d to your computer and use it in GitHub Desktop.
Save rudylacrete/ab057f662763007a930d to your computer and use it in GitHub Desktop.
init.d script to start nodejs application with forever
### BEGIN INIT INFO
# Provides: mynodejsapplication
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Should-Start: $local_fs
# Should-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Script for My Node Application
# Description: Script for My Node Application
### END INIT INFO
# An application name to display in echo text.
# An application name to display in echo text.
# NAME="My Application"
# The full path to the directory containing the node and forever binaries.
# NODE_BIN_DIR=/home/node/local/node/bin
# Set the NODE_PATH to the Node.js main node_modules directory.
# NODE_PATH=/home/node/local/node/lib/node_modules
# The directory containing the application start Javascript file.
# APPLICATION_DIRECTORY=/home/node/my-application
# The application start Javascript filename.
# APPLICATION_START=start-my-application.js
# Process ID file path.
# PIDFILE=/var/run/my-application.pid
# Log file path.
# LOGFILE=/var/log/my-application.log
NAME="MYNodeJS"
NODE_BIN_DIR=/usr/bin
NODE_PATH=/usr/local/lib/node_modules/
APPLICATION_DIRECTORY=/data/app
APPLICATION_START=index.js
PIDFILE=/var/run/$NAME.pid
LOGFILE=/var/log/$NAME.log
PATH=$NODE_BIN_DIR:$PATH
export NODE_PATH=$NODE_PATH
start() {
echo "Starting $NAME"
/usr/local/bin/forever --pidFile $PIDFILE --sourceDir $APPLICATION_DIRECTORY \
-a -l $LOGFILE --minUptime 5000 --spinSleepTime 2000 \
start $APPLICATION_START &
RETVAL=$?
}
stop() {
if [ -f $PIDFILE ]; then
echo "Shutting down $NAME"
forever stop $APPLICATION_START
RETVAL=$?
else
echo "$NAME is not running."
RETVAL=0
fi
}
restart() {
echo "Restarting $NAME"
stop
start
}
status() {
echo "Status for $NAME:"
/usr/local/bin/forever list
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
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