Skip to content

@nariyu /nodejs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
node.js init.d script for CentOS
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js /home/nodejs/sample/app.js
#
. /etc/rc.d/init.d/functions
USER="nodejs"
DAEMON="/home/nodejs/.nvm/v0.4.10/bin/node"
ROOT_DIR="/home/nodejs/sample"
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 -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
@jahrmando

Thanks for your script, very useful

@mattwilliamson

I was able to retrofit this into a generic python daemon. Very helpful, thanks!

@CodeMonkeyG

Got me going in the right direction. Thanks!

@XPerianer

For Google Coder on the Raspberry Pi, this was very usefull. Thank you very much!

@brunoalmeida406

Hi,

Change the start area, to add this content " && echo ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}' > $LOCK_FILE".

The result is "[ $RETVAL -eq 0 ] && touch $LOCK_FILE && echo ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}' > $LOCK_FILE
else"

This is necessary to analise PID of process. I'm install the "monit" to monitoring and restart node.js process. There is need for PID in the "LOCK_FILE".

Regards,
Bruno Almeida At Rivendel Tecnologia - From Brazil

@26medias

Would have been useful to comment the lines to change, and how to use it and start/stop it.

@edwardsmarkf

thank you very much. your script worked flawlessly with practically no changes.

@ihsanberahim

Thx! but i prefer amend abit to make it better

runuser -l "$USER" -c " cd $ROOT_DIR; $DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
@sebastianandreasson

Had some problems getting this to work on Centos 7, needed to change line 24 to
$DAEMON $SERVER >> $LOG_FILE 2>&1 && echo_success || echo_failure
and also forgot to do sudo chmod +x /etc/init.d/myscript

@dtate888

Hi nariyu, just a legal clarification, we can use this snippet freely as we see fit, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.