Newcomers to Node.js will have run into this issue a couple of times. You get your setup working, you’ve built a little application and on your server you start your Node app, but then you close the terminal window and your application stops.
When I first started using Node.js, this is the first thing I Googled, “How do I keep a Node.js app running after I close the terminal console?”
The approach we are going to be detailing here uses the Node package Forever and the Linux upstart package. By default Upstart should be shipped with most distros of Linux, but if it is not, you can install it using your Linux package manager (yum, apt-get).
Forever is essentially a simplistic module that will spin up your Node.js app and restart it if needed. When it says forever, it literally means forever (until you say stop anyway).
Installing Forever
Assuming that you’ve got Node.js installed, open up your terminal window and connect to your server. We now want to install Forever globally so type: npm install -g forever – that’s it for this step.
Writing an Upstart script
This is the bread and butter. This will allow us to treat our Node application like a system service. I use Ubuntu, so some things might differ in my instructions.
Upstart scripts will be living in this directory: /etc/init and our script will have a .conf extension. So for this example we will create a file called nodeapp.conf in our /etc/init directory
See below for our Upstart script, change the appropriate paths for Node and other things. Save the below script as /etc/init/myapp.conf or whatever name you choose.
Please also don’t forget to change all instances of nodeapp to your application name and tell the script the proper path to your main Node app Javascript file.
#!upstart
description "My cool Node.js process that will run FOREVER, ahahahahaha"
start on filesystem and started networking
stop on shutdown
# You need this so Upstart reports the proper process ID of Node and not Forever.
expect fork
# Monitor Forever
respawn
respawn limit 5 30
# Node paths (change to suit your installation)
# The full path to the directory containing the node and forever binaries.
env NODE_BIN_DIR="/usr/local/bin"
# Set the NODE_PATH to the Node.js main node_modules directory
env NODE_PATH="/usr/local/lib/node_modules"
# The main file that starts our app (usually called server.js or app.js)
env APPLICATION_PATH="/var/www/nodeapp.com/public_html/server.js"
# Process ID file path
env PIDFILE="/var/run/nodeapp.pid"
# Log file path
env LOG="/var/log/nodeapp.log"
# Forever settings to prevent the application spinning up constantly if it fails on launch.
env MIN_UPTIME="5000"
env SPIN_SLEEP_TIME="2000"
# Node specific environment variables
env NODE_ENV=production
env PORT=3001
script
# Add our Node stuff to the main path
PATH=$NODE_BIN_DIR:$PATH
# Prevent Forever from bringing down our server by repeatedly attempting to spin
* up our Node app if it fails
exec forever \
--pidFile $PIDFILE \
-a \
-l $LOG \
--minUptime $MIN_UPTIME \
--spinSleepTime $SPIN_SLEEP_TIME \
start $APPLICATION_PATH
end script
pre-stop script
PATH=$NODE_BIN_DIR:$PATH
exec forever stop $APPLICATION_PATH
end script
Using Your Upstart Script
There are only a few commands to remember and if you’re familiar with starting and stopping services already, this won’t be unusual to you. Remember to replace nodeapp with the name of your script. So if you created a file called /etc/init/multiplayer-chess.conf you would replace nodeapp with multiplayer-chess and so on.
start nodeapp
stop nodeapp
restart nodeapp
status nodeapp
The above commands are pretty self-explanatory; start will start your app as a service, stop will stop it running, restart will restart it and status will tell you whether or not it is currently running or not.
you could also use PM2 : ) !
have a look at:
– https://github.com/Unitech/pm2
– http://devo.ps/blog/goodbye-node-forever-hello-pm2/
– http://npm-stat.com/charts.html?package=pm2
PM2 is definitely a great choice, better for testing and stats monitoring as well. I just used Forever as an example more for newbies coming into Node.js 🙂