Skip to content

Instantly share code, notes, and snippets.

@Miserlou
Created January 12, 2012 04:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miserlou/1598685 to your computer and use it in GitHub Desktop.
Save Miserlou/1598685 to your computer and use it in GitHub Desktop.
/etc/init.d/nodeapp
#!/bin/bash
DIR=/var/www/YOUR_NODE_APP_GOES_HERE
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NODE_PATH=/usr/local/lib/node_modules
NODE=/usr/local/bin/node
test -x $NODE || exit 0
function start_app {
NODE_ENV=production nohup "$NODE" "$DIR/YOUR_APP_SERVER_FILE.js" 1>>"$DIR/logs/YOUR_APP_NAME.log" 2>&1 &
echo $! > "$DIR/pids/YOUR_APP.pid"
}
function stop_app {
kill `cat $DIR/pids/YOUR_APP.pid`
}
case $1 in
start)
start_app ;;
stop)
stop_app ;;
restart)
stop_app
start_app
;;
*)
echo "usage: YOUR_APP_NAME {start|stop}" ;;
esac
exit 0
@Cramertech
Copy link

That worked perfectly for me! I've been searching for a long time and your solution is the only one that worked out.

I only had to change my NODE and NODE_PATH variables to exclude the /local folder. I'm not sure why my system was different, but it wasn't in local.

Thank you so much for posting this.

PS - would you know how I can make the process run as a custom service user like the one I made for Node - similar to how Apache runs under the apache service user?

@freekode
Copy link

@umpitygrumpity an example:

su specuser -c "nohup npm start /home/user/app/ 1>>/home/user/app/out.log 2>&1 &"

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