Skip to content

Instantly share code, notes, and snippets.

@cdparra
Created December 3, 2016 05:25
Show Gist options
  • Save cdparra/4e684b37ca60f370135b32f2bc39b611 to your computer and use it in GitHub Desktop.
Save cdparra/4e684b37ca60f370135b32f2bc39b611 to your computer and use it in GitHub Desktop.
Init script for grunt apps
#!/bin/sh
APP_DIR='/path/to/gruntapp/home';
CMD="/usr/local/bin/grunt"
CMD_ARGS="server --base $APP_DIR --gruntfile $APP_DIR/Gruntfile.js"
PID_FILE=$APP_DIR/app.pid
LOG_FILE=$APP_DIR/app.log
USER="appuser"
GROUP="appgroup"
set -e
. /lib/lsb/init-functions
start_app (){
if [ -f $PID_FILE ]
then
echo "$PID_FILE exists, process is already running or crashed"
exit 1
else
echo "Starting node app..."
start-stop-daemon -v --start --chuid "$USER:$GROUP" --background --pidfile $PID_FILE -d $APP_DIR --exec $CMD -- $CMD_ARGS 1>$LOG_FILE 2>$LOG_FILE
fi
}
stop_app (){
if [ ! -f $PID_FILE ]
then
echo "$PID_FILE does not exist, process is not running"
exit 1
else
echo "Stopping $APP_DIR/$NODE_APP $CMD ..."
echo "Killing `cat $PID_FILE`"
kill `cat $PID_FILE`;
rm -f $PID_FILE;
echo "Node stopped"
fi
}
#We need this function to ensure the whole process tree will be killed
killtree() {
local _pid=$1
local _sig=${2-TERM}
for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
killtree ${_child} ${_sig}
done
kill -${_sig} ${_pid}
}
status() {
status_of_proc -p $PID_FILE "" "gruntapp" && exit 0 || exit $?
}
case "$1" in
start)
start_app
;;
stop)
stop_app
;;
restart)
stop_app
start_app
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment