Skip to content

Instantly share code, notes, and snippets.

@mitchallen
Created June 15, 2012 18:21
Show Gist options
  • Save mitchallen/2938003 to your computer and use it in GitHub Desktop.
Save mitchallen/2938003 to your computer and use it in GitHub Desktop.
Example script for running a node app as a service under RHEL6 (sudo start. See the comments for usage.
#!upstart
description "myapp nodejs server"
author "Mitch Allen"
start on startup
stop on shutdown
respawn
expect fork
expect daemon
nice -5
script
export HOME="/export/home/MY-USER"
echo $$ > /var/run/myapp.pid
su --session-command="/usr/bin/nodejs $HOME/test/server.js >> /var/log/myapp.sys.log 2>&1" root &
end script
pre-start script
# Date format same as (new Date()).toISOString() for consistency
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/myapp.sys.log
end script
pre-stop script
rm /var/run/myapp.pid
echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/myapp.sys.log
end script
@mitchallen
Copy link
Author

Note that this is for RHEL6.

Add the contents of this gist to a file and edit for your nodejs deployment:

sudo vim /etc/init/myapp.conf

Example usage:

Start, view status, stop:

sudo start myapp
sudo status myapp
sudo stop myapp

Start, view process, stop, view process:

sudo start myapp
ps aux | grep nodejs
sudo stop myapp
ps aux | grep nodejs

Start then tail the log (allowing you to monitor the output of your node app):

sudo start myapp
sudo tail -f /var/log/myapp.sys.log

Tail the log from a remote client (substituting USER and SERVER for your environment):

ssh USER@SERVER tail -f /var/log/myapp.sys.log

References:

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