Skip to content

Instantly share code, notes, and snippets.

@inadarei
Created June 24, 2011 18:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inadarei/1045395 to your computer and use it in GitHub Desktop.
Save inadarei/1045395 to your computer and use it in GitHub Desktop.
Daemonize Node.js
#!/bin/bash
## Node.js App Launcher
## Usage: ./nodedaemon.sh /home/apps/projectname/app.js
## Baesed on: http://pastebin.com/wNJNkbjg
# name=`basename $1` # For when/if we want to use scriptname
name=`dirname $1 | xargs -0 basename` # When/if we want to use last folder name as project name
cd `dirname $1`
pidfile=/var/run/$name.pid
if [ -f "$pidfile" ]; then
pid=`cat $pidfile`
running=`ps p $pid |wc -l`
if [ $running -eq 1 ]; then
pid=
fi
else
pid=
fi
case $2 in
start)
if [ "$pid" = "" ]; then
nohup /usr/local/bin/node $1.js $1.conf 2>&1 1>>/var/log/$name &
echo $! > $pidfile
fi
$0 $1 status
;;
stop)
if [ "$pid" = "" ]; then
echo Not running
else
echo Stopping $name
kill $pid
fi
;;
restart)
$0 $1 stop
$0 $1 start
;;
status)
if [ "$pid" = "" ]; then
echo Stopped
else
echo Running with PID $pid
fi
;;
*)
echo $1 "stop|start|restart|status"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment