Skip to content

Instantly share code, notes, and snippets.

@felixebert
Created May 13, 2016 15:22
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 felixebert/37ae8742da20e4d7a38180910e56faae to your computer and use it in GitHub Desktop.
Save felixebert/37ae8742da20e4d7a38180910e56faae to your computer and use it in GitHub Desktop.
nodejs forever init-script
#!/bin/bash
### BEGIN INIT INFO
# Provides: nodeapp
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Node App
### END INIT INFO
# processname: node
# pidfile: /var/run/nodeapp.pid
# logfile: /var/log/nodeapp.log
#
# Based on https://gist.github.com/jinze/3748766
#
# To use it as service on Ubuntu:
# sudo cp nodeapp.sh /etc/init.d/nodeapp
# sudo chmod a+x /etc/init.d/nodeapp
# sudo update-rc.d nodeapp defaults
#
# Then use commands:
# service nodeapp <command (start|stop|etc)>
NAME=nodeapp # Unique name for the application
SOUREC_DIR=/opt/nodeapp # Location of the application source
COMMAND=node # Command to run
SOURCE_NAME=app.js # Name os the applcation entry point script
USER=jenkins # User for process running
NODE_ENVIROMENT=production # Node environment
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever=forever
start() {
export NODE_ENV=$NODE_ENVIROMENT
echo "Starting $NAME node instance : "
touch $logfile
chown $USER $logfile
touch $pidfile
chown $USER $pidfile
sudo -H -u $USER $forever start --uid "$NAME" --pidFile $pidfile -l $logfile -a -m 5 --sourceDir $SOUREC_DIR -c $COMMAND $SOURCE_NAME
RETVAL=$?
}
restart() {
echo -n "Restarting $NAME node instance : "
sudo -H -u $USER $forever restart $NAME
RETVAL=$?
}
status() {
echo "Status for $NAME:"
sudo -H -u $USER $forever list
RETVAL=$?
}
stop() {
echo -n "Shutting down $NAME node instance : "
sudo -H -u $USER $forever stop $NAME
}
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