Skip to content

Instantly share code, notes, and snippets.

@deniak
Forked from jinze/forever-initd-example.sh
Last active March 14, 2016 14:32
Show Gist options
  • Save deniak/b5d7e0fb9cd2dce4aa39 to your computer and use it in GitHub Desktop.
Save deniak/b5d7e0fb9cd2dce4aa39 to your computer and use it in GitHub Desktop.
typo
#!/bin/bash
#
# initd-example Node init.d
# update of https://gist.github.com/jinze/3748766
#
# description: Node init.d example
# processname: node
# pidfile: /var/run/initd-example.pid
# logfile: /var/log/initd-example.log
#
# Source function library.
. /lib/lsb/init-functions
NAME=initd-example # Unique name for the application
NODE_ENV=production # Node environment
PORT=1234 # Port (in this case the application uses process.env.PORT to set the port)
INSTANCE_DIR=/var/www/$NAME # Location of the application source
COMMAND=coffee # Command to run
SOURCE_NAME=app.coffee # Name of the applcation entry point script
user=apache
pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
node=node
forever=forever
awk=awk
sed=sed
start() {
echo "Starting $NAME node instance: "
if [ "$id" = "" ]; then
# Create the log and pid files, making sure that the target use has access to them
touch $logfile
chown $user $logfile
touch $pidfile
chown $user $pidfile
# Launch the application
start_daemon
$forever start --pidFile $pidfile -l $logfile -a -d $COMMAND $INSTANCE_DIR/$SOURCE_NAME
RETVAL=$?
else
echo "Instance already running"
RETVAL=0
fi
}
restart() {
echo -n "Restarting $NAME node instance : "
if [ "$id" != "" ]; then
$forever restart -l $logfile -a -d $COMMAND $INSTANCE_DIR/$SOURCE_NAME
RETVAL=$?
else
start
fi
}
stop() {
echo -n "Shutting down $NAME node instance : "
if [ "$id" != "" ]; then
$forever stop $(pidofproc -p $pidfile)
else
echo "Instance is not running";
fi
RETVAL=$?
}
id=$(ps -eo pid,command | grep -v grep | grep "$INSTANCE_DIR/$SOURCE_NAME" | grep -v "`pidofproc -p $pidfile`")
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile}
;;
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