Skip to content

Instantly share code, notes, and snippets.

@antonpiatek
Forked from jinze/forever-initd-example.sh
Last active May 17, 2016 17:32
Show Gist options
  • Save antonpiatek/5f024a9c4da2b84fb7ed to your computer and use it in GitHub Desktop.
Save antonpiatek/5f024a9c4da2b84fb7ed to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# An init.d script for running a Node.js process as a service using Forever as
# the process monitor. For more configuration options associated with Forever,
# see: https://github.com/nodejitsu/forever
#
# This was written for Debian distributions such as Ubuntu, but should still
# work on RedHat, Fedora, or other RPM-based distributions, since none of the
# built-in service functions are used. So information is provided for both.
#
### BEGIN INIT INFO
# Provides: my-nodejs-app
# 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: My NodeJS App
# Description: My NodeJS App
### END INIT INFO
#
### BEGIN CHKCONFIG INFO
# chkconfig: 2345 55 25
# description: My NodeJS App
### END CHKCONFIG INFO
#
# Based on:
# https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/
# https://gist.github.com/3748766
# https://github.com/hectorcorrea/hectorcorrea.com/blob/master/etc/forever-initd-hectorcorrea.sh
# https://www.exratione.com/2011/07/running-a-nodejs-server-as-a-service-using-forever/
#
# The example environment variables below assume that Node.js is installed by
# building from source with the standard settings and that make install was run as root
# Add $PATH additions if required
# An application name to display in echo text.
NAME="my nodejs app"
# The application startup Javascript file path.
APPLICATION_PATH="/.../app.js"
# Log file path.
LOGFILE="/var/log/node/myapp.log"
# Forever settings to prevent the application spinning if it fails on launch.
MIN_UPTIME="5000"
SPIN_SLEEP_TIME="2000"
# User to run as - you should not run apps as root, and the user should have a homedir
USER=node
#argument options you might want to set, e.g. NODE_ENV=production or a PORT option
ENV_ARGS="PORT=3000"
#Additional forever args, probably only ever going to need something like "-c coffee"
FOREVER_ARGS=""
checkHome(){
# Check that the users homedir exists or warn, as forever really likes a homedir
# Do this as that user in case you want to lock it down tight
eval TESTHOME=~$USER
if ! sudo -u $USER test -d $TESTHOME; then
echo "WARNING Homedir $TESTHOME for user $USER doesn't seem to exist"
echo "This may casuse forever to fail to work properly"
fi
}
checkHome
start() {
echo "Starting $NAME"
# 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.
#set logfiles as writable by target user
touch $LOGFILE
chown $USER $LOGFILE
# launch app as user by launching forever with a specifc uid name to identify it
sudo -u $USER $ENV_ARGS forever \
$FOREVER_ARGS \
-a \
-l $LOGFILE \
--minUptime $MIN_UPTIME \
--spinSleepTime $SPIN_SLEEP_TIME \
--uid "$NAME" \
start $APPLICATION_PATH 2>&1 > /dev/null
RETVAL=$?
}
stop() {
echo "Shutting down $NAME"
# Tell Forever to stop the process.
sudo -u $USER forever \
stop $APPLICATION_PATH 2>&1 > /dev/null
RETVAL=$?
}
restart() {
stop
start
}
# List all running jobs that forever knows about
list() {
sudo -u $USER forever list
}
status() {
sudo -u $USER forever list | grep -q "$NAME"
if [ "$?" -eq "0" ]; then
echo "$NAME is running."
RETVAL=0
else
echo "$NAME is not running."
RETVAL=3
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
list)
list
;;
*)
echo "Usage: {start|stop|status|restart|list}"
exit 1
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment