Skip to content

Instantly share code, notes, and snippets.

@hengkiardo hengkiardo/app.sh

forked from anonymous/app.sh
Last active Aug 29, 2015
Embed
What would you like to do?
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js app server script
#
. /etc/rc.d/init.d/functions
USER="root"
DAEMON="/usr/bin/node"
ROOT_DIR="/var/www/html/honorbound/latest"
SCRIPT="$(basename $0)"
PIDFILE="/var/run/$SCRIPT.pid"
SERVER="$ROOT_DIR/main.js"
LOG_FILE="/var/log/node/node.log"
SHUTDOWN_WAIT=20
app_pid()
{
echo `ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
}
do_start()
{
echo -n $"Starting $SERVER: "
pid=$(app_pid)
if [ -n "$pid" ]
then
echo "App server is already running (pid: $pid)"
else
echo "App server is not running - starting it up!"
runuser -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE 2>&1 &" && echo_success || echo_failure
pid=$(app_pid)
echo "App server started, runing at $pid"
echo $pid > ${PIDFILE}
RETVAL=$?
echo
[ $RETVAL -eq 0 ]
fi
return 0
}
do_wait()
{
echo "Waiting for process to exit";
pid=$(app_pid)
if [ -n "$pid" ]
then
let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
do
echo -n -e "\nwaiting for processes to exit";
sleep 1
let count=$count+1;
done
fi
return 0
}
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
if [ -f ${PIDFILE} ]; then
rm ${PIDFILE}
fi
RETVAL=$?
echo
[ $RETVAL -eq 0 ]
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_wait
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
You can’t perform that action at this time.