Skip to content

Instantly share code, notes, and snippets.

@ellman
Last active August 31, 2020 12:08
Show Gist options
  • Save ellman/8581167 to your computer and use it in GitHub Desktop.
Save ellman/8581167 to your computer and use it in GitHub Desktop.
Run Nodejs as upstart service on Ubuntu or Centos
#Upstart Service For Nodejs in /etc/init
description 'nodejs example upstart'
author 'Yonatan Ellman (blog.nodeside.com) adpated from ivarprudnikov.com'
env NAME=TEST
env LOG_FILE=/var/log/nodejs-test.log
env USER=nodejs
env NODE_BIN=/usr/local/bin/node
env NODE_ENV="development"
env PORT=3001
env DIRECTORY=/var/www/yourapp
env APP=app.js
start on runlevel [23]
stop on shutdown
# Respawn in case of a crash, with default parameters
respawn
script
cd $DIRECTORY
su $USER
# Make sure logfile exists and can be written by the user we drop privileges to
touch $LOG_FILE
chown $USER:$USER $LOG_FILE
# recommended approach in case of su/sudo usage so that service does not fork
exec sudo -u $USER PORT=$PORT NODE_ENV=$NODE_ENV $NODE_BIN $DIRECTORY/$APP >> $LOG_FILE 2>&1
end script
post-start script
echo "app $NAME $NODE_ENV post-start event" >> $LOG_FILE
end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment