I connect to the linux server via putty SSH. I tried to run it as a background process like this:
$ node server.js &
However, after 2.5 hrs the terminal becomes inactive and the process dies. Is there anyway I can keep the process alive even with the terminal disconnected?
Edit 1
Actually, I tried nohup
, but as soon as I close the Putty SSH terminal or unplug my internet, the server process stops right away.
Is there anything I have to do in Putty?
Edit 2 (on Feb, 2012)
There is a node.js
module, forever. It will run node.js server as daemon service.
Simple solution (if you are not interested in coming back to the process, just want it to keep running):
Powerful solution (allows you to reconnect to the process if it is interactive):
You can then detach by pressing Ctrl+a+d and then attach back by running
screen -r
Also consider the newer alternative to screen, tmux.
nohup
will allow the program to continue even after the terminal dies. I have actually had situations wherenohup
prevents the SSH session from terminating correctly, so you should redirect input as well:Depending on how
nohup
is configured, you may also need to redirect standard output and standard error to files.nohup node server.js > /dev/null 2>&1 &
nohup
means: Do not terminate this process even when the stty is cut off.> /dev/null
means: stdout goes to /dev/null (which is a dummy device that does not record any output).2>&1
means: stderr also goes to the stdout (which is already redirected to/dev/null
). You may replace &1 with a file path to keep a log of errors, e.g.:2>/tmp/myLog
&
at the end means: run this command as a background task.Have you read about the nohup command?
This is an old question, but is high ranked on Google. I almost can't believe on the highest voted answers, because running a node.js process inside a screen session, with the
&
or even with thenohup
flag -- all of them -- are just workarounds.Specially the screen/tmux solution, which should really be considered an amateur solution. Screen and Tmux are not meant to keep processes running, but for multiplexing terminal sessions. It's fine, when you are running a script on your server and want to disconnect. But for a node.js server your don't want your process to be attached to a terminal session. This is too fragile. To keep things running you need to daemonize the process!
There are plenty of good tools to do that.
PM2: http://pm2.keymetrics.io/
One big advantage I see in favor of PM2 is that it can generate the system startup script to make the process persist between restarts:
Where
platform
can beubuntu|centos|redhat|gentoo|systemd|darwin|amazon
.forever.js: https://github.com/foreverjs/forever
Init scripts:
I'm not go into detail about how to write a init script, because I'm not an expert in this subject and it'd be too long for this answer, but basically they are simple shell scripts, triggered by OS events. You can read more about this here
Docker:
Just run your server in a Docker container with
-d
option and, voilá, you have a daemonized node.js server!Here is a sample Dockerfile (from node.js official guide):
Then build your image and run your container:
Hope this helps somebody landing on this page. Always use the proper tool for the job. It'll save you a lot of headaches and over hours!
I have this function in my shell rc file, based on @Yoichi's answer:
You can use it this way: