Created
June 24, 2011 18:40
-
-
Save inadarei/1045395 to your computer and use it in GitHub Desktop.
Daemonize Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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