Skip to content

Instantly share code, notes, and snippets.

@renebakx
Created August 29, 2014 15:26
Show Gist options
  • Save renebakx/c18b5aab21b06150e12e to your computer and use it in GitHub Desktop.
Save renebakx/c18b5aab21b06150e12e to your computer and use it in GitHub Desktop.
init.d script fo running prerender.io as a service with logging (redhat/centos)
#!/bin/sh
#
# chkconfig: 35 99 99
# description: prerender.js server for the given user
#
. /etc/rc.d/init.d/functions
APPNAME="Prerender"
USER="<user that runs prerender>"
DAEMON="/usr/local/node"
ROOT_DIR="<path to prerender>"
SCRIPT="$(basename $0)"
PIDFILE="/var/run/$SCRIPT.pid"
SERVER="$ROOT_DIR/server.js"
LOG_FILE="/var/log/prerender.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 "$APPNAME is already running (pid: $pid)"
else
if [ ! -w $LOG_FILE ]
then
if [ ! -e $LOG_FILE ]
then
touch $LOG_FILE
fi
chown $USER $LOG_FILE
fi
echo "$APPNAME is not running - starting it up!"
runuser --shell=/bin/bash -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE 2>&1 &"
pid=$(app_pid)
echo $pid > ${PIDFILE}
do_status
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 $APPNAME : "
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 ]
}
do_status()
{
pid=$(app_pid)
if [ -n "$pid" ]
then
echo -n "$APPNAME is running (pid: $pid)"
echo_success
else
echo -n "$APPNAME is not running"
echo_failure
fi
echo
return 0
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
status)
do_status
;;
restart)
do_stop
do_wait
do_start
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
RETVAL=1
esac
exit $RETVAL
@sugov5
Copy link

sugov5 commented Jun 3, 2017

Thanks!!!

@FamousMai
Copy link

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment