How to run node.js app forever when console is closed?
Two answers: One for Windows, one for *nix:
On Windows, you can use the start
command to start the process disconnected from your instance of cmd.exe
:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUP
signal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
Starting disconnected from the console is easy: Usually, you just put an ampersand (&
) at the end of the command line:
# Keep reading, don't just grab this and use it
node example.js &
But the above doesn't protect the process from HUP
signals. The program may or may not receive HUP
when you close the shell (console), depending on a shell option called huponexit
. If huponexit
is true
, the process will receive HUP
when the shell exits and will presumably terminate.
huponexit
defaults to false
on the various Linux variants I've used, and in fact I happily used the above for years until coderjoe and others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexit
being false
.
To avoid the possibility that huponexit
might be true
in your environment, explicitly use nohup
. nohup
runs the process immune from HUP
signals. You use it like this:
nohup node example.js > /dev/null &
or
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.out
file containing the output from stdout
and stderr
. (By default, nohup
redirects stderr
to stdout
, and if stdout
is outputting to a terminal, it redirects that to nohup.out
. nohup
also redirects stdin
if it's receiving from a terminal, so we don't have to do that. See man nohup
or info coreutils 'nohup invocation'
for details.)
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above does work for simple cases.
I would definitely recommend pm2
npm install -g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
Install upstart:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp