Skip to content

Instantly share code, notes, and snippets.

@emiller42
Created October 28, 2013 04:44
Show Gist options
  • Save emiller42/7191554 to your computer and use it in GitHub Desktop.
Save emiller42/7191554 to your computer and use it in GitHub Desktop.
init.d script to run Ghost under a service account.
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js /home/ghost/ghost/index.js
#
. /etc/rc.d/init.d/functions
USER="ghost"
DAEMON="/usr/local/bin/node"
ROOT_DIR="/home/ghost/ghost"
SERVER="$ROOT_DIR/index.js"
LOG_FILE="$ROOT_DIR/log/ghost.log"
NODE_ENV="production"
LOCK_FILE="/var/lock/subsys/ghost"
do_start()
{
if [ ! -f "$LOCK_FILE" ] ; then
echo -n $"Starting $SERVER: "
runuser -l "$USER" -c "cd $ROOT_DIR && NODE_ENV=$NODE_ENV $DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
else
echo "$SERVER is locked."
RETVAL=1
fi
}
do_stop()
{
echo -n $"Stopping $SERVER: "
pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment