Skip to content

Instantly share code, notes, and snippets.

@jmtagarro
Created March 29, 2012 13:47
Show Gist options
  • Save jmtagarro/2237629 to your computer and use it in GitHub Desktop.
Save jmtagarro/2237629 to your computer and use it in GitHub Desktop.
node.js init.d script for CentOS with pidfile
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js /home/nodejs/sample/app.js
#
. /etc/rc.d/init.d/functions
# Creamos un fichero PID para monit
SCRIPT="$(basename $0)"
PIDFILE="/var/run/$SCRIPT.pid"
ps -fe | grep "$SCRIPT" | head -n1 | cut -d" " -f 6 > ${PIDFILE}
USER="node"
DAEMON="/usr/local/bin/node"
ROOT_DIR="/var/www/vhosts/mydomain.com/mywebapp"
SERVER="$ROOT_DIR/app.js"
LOG_FILE="$ROOT_DIR/app.js.log"
LOCK_FILE="/var/lock/subsys/node-server"
do_start()
{
if [ ! -f "$LOCK_FILE" ] ; then
echo -n $"Starting $SERVER: "
runuser -l "$USER" -c "$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 -fe | grep "$SCRIPT" | head -n1 | cut -d" " -f 6`
kill -9 $PID > /dev/null 2>&1 && echo_success || echo_failure
if [ -f ${PIDFILE} ]; then
rm ${PIDFILE}
fi
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
</pre>
@Trott
Copy link

Trott commented Mar 18, 2013

You have a dangling </pre> on line 67. Otherwise, I'm off to give this a spin! Thanks!

@jeffmccune
Copy link

Why kill -9 on stop instead of kill -TERM? Kill -9 doesn't give the process a chance to clean up and shut down.

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