Skip to content

Instantly share code, notes, and snippets.

@faxm0dem
Created January 24, 2014 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save faxm0dem/8594680 to your computer and use it in GitHub Desktop.
Save faxm0dem/8594680 to your computer and use it in GitHub Desktop.
node.js fedora style init script
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js /home/nodejs/sample/app.js
#
. /etc/rc.d/init.d/functions
USER="node"
DAEMON="/usr/bin/node"
APP_NAME="sample"
CONFIGFILE=/etc/sysconfig/node
# source config info
[ -r ${CONFIGFILE} ] && . ${CONFIGFILE}
APP_DIR="/home/node/$APP_NAME"
SERVER="$APP_DIR/app.js"
LOG_FILE="$APP_DIR/app.js.log"
LOCK_FILE="/var/lock/subsys/node-$APP_NAME"
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=$(pgrep -f "$DAEMON $APP_DIR")
kill $pid && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}
case "$1" in
status)
pgrep -lf "$DAEMON $APP_DIR" || echo "Node.js app $APP_NAME is not running according do pstable"
;;
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
@faxm0dem
Copy link
Author

removed some uuoc from ''stop'' and added ''status''
added support for sysconfdir

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