Skip to content

Instantly share code, notes, and snippets.

@adsahay
Created May 26, 2014 17:28
Show Gist options
  • Select an option

  • Save adsahay/1827d6475c25d58fc880 to your computer and use it in GitHub Desktop.

Select an option

Save adsahay/1827d6475c25d58fc880 to your computer and use it in GitHub Desktop.
Upstart script to run
#!upstart
# upstart script for nodejs+forever
# ref: https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/
description "upstart + nodejs + forever"
start on startup
stop on shutdown
expect fork
respawn
respawn limit 5 30
env NODE_BIN_DIR="/usr/bin"
env APPLICATION_DIR="/path/to/app/dir"
env APPLICATION_PATH="app.js"
env PIDFILE="/var/run/app.pid"
env LOG="/var/log/app.log"
env MIN_UPTIME="5000"
env SPIN_SLEEP_TIME="2000"
script
# Add the node executables to the path, which includes Forever if it is
# installed globally, which it should be.
PATH=$NODE_BIN_DIR:$PATH
# The minUptime and spinSleepTime settings stop Forever from thrashing if
# the application fails immediately on launch. This is generally necessary
# to avoid loading development servers to the point of failure every time
# someone makes an error in application initialization code, or bringing
# down production servers the same way if a database or other critical
# service suddenly becomes inaccessible.
chdir $APPLICATION_DIR
exec forever \
--pidFile $PIDFILE \
-a \
-l $LOG \
--minUptime $MIN_UPTIME \
--spinSleepTime $SPIN_SLEEP_TIME \
start $APPLICATION_PATH
end script
pre-stop script
# Add the node executables to the path.
PATH=$NODE_BIN_DIR:$PATH
# Here we're using the pre-stop script to stop the Node.js application
# process so that Forever is given a chance to do its thing and tidy up
# its data. Note that doing it this way means that each application that
# runs under Forever must have a different start file name, regardless of
# which directory it is in.
chdir $APPLICATION_DIR
exec forever stop $APPLICATION_PATH
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment