Skip to content

Example API Server Init Script

Dan Leehr edited this page Jul 1, 2015 · 2 revisions
#!/bin/bash
### BEGIN INIT INFO
# Provides:          /data/webapps/fcdb-api/server.js
# 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 /data/webapps/fcdb-api/server.js
# Description:       /data/webapps/fcdb-api/server.js
### END INIT INFO
#
# initd a node app
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#

# Source function library.
. /etc/init.d/functions

pidFile="/var/run/fcdb-api.pid"
logFile="/var/log/fcdb-api.log"

command="forever"
nodeApp="/data/webapps/fcdb-api/server.js"
config="/data/webapps/fcdb-api/.fcdb-api-config"
foreverApp="/usr/bin/forever"

start() {
	echo "Starting $nodeApp"

	# Notice that we change the PATH because on reboot
   # the PATH does not include the path to node.
   # Launching forever with a full path
   # does not work unless we set the PATH.
   PATH=/usr/local/bin:$PATH
	export NODE_ENV=production
   #PORT=80
   # Read config
   . $config
   $foreverApp start --pidFile $pidFile -l $logFile -a -d  $nodeApp
   RETVAL=$?
}

restart() {
	echo -n "Restarting $nodeApp"
	$foreverApp restart $nodeApp
	RETVAL=$?
}

stop() {
	echo -n "Shutting down $nodeApp"
   $foreverApp stop $nodeApp
   RETVAL=$?
}

status() {
   echo -n "Status $nodeApp"
   $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