Skip to content

Instantly share code, notes, and snippets.

@ryankurte
Last active August 29, 2015 14:23
Show Gist options
  • Save ryankurte/119a2d716428d1ea1ca8 to your computer and use it in GitHub Desktop.
Save ryankurte/119a2d716428d1ea1ca8 to your computer and use it in GitHub Desktop.
Node.js init.d script
#!/bin/bash
### BEGIN INIT INFO
# Provides: [APP_NAME_HERE]
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Forever running [APP_NAME_HERE]
# Description: Forever running [APP_NAME_HERE]
### END INIT INFO
#
# init.d script for a node app
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
# And the generator at: https://github.com/yeoman/generator-node
# Modified to run forever as a non superuser
#
# Source function library.
. /lib/lsb/init-functions
user="USER_HERE"
appName="APP_NAME_HERE"
workingDir="/var/$appName"
pidFile="$workingDir/$appName.pid"
logFile="$workingDir/$appName.log"
nodePath="/opt/node/bin"
nodeApp="${nodePath}/$appName"
command="${nodePath}/node"
foreverApp="${nodePath}/forever"
export PATH=$PATH:$nodePath
start() {
echo "Starting $nodeApp"
su -l $user -c "PATH=$PATH:$nodePath NODE_ENV=production $foreverApp start --workingDir $workingDir --pidFile $pidFile --logFile $logFile -a -d $nodeApp"
}
restart() {
echo "Restarting $nodeApp"
su -l $user -c "PATH=$PATH:$nodePath $foreverApp restart $nodeApp"
RETVAL=$?
}
stop() {
echo "Shutting down $nodeApp"
su -l $user -c "PATH=$PATH:$nodePath $foreverApp stop $nodeApp"
RETVAL=$?
}
status() {
echo "Status $nodeApp"
su -l $user -c "PATH=$PATH:$nodePath $foreverApp 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