Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/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

This comment has been minimized.

Copy link

@Cramertech Cramertech commented Apr 3, 2013

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

This comment has been minimized.

Copy link

@freekode freekode commented May 14, 2013

@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