How to run Node.js as a background process and never die?
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.
Simple solution (if you are not interested in coming back to the process, just want it to keep running):
nohup node server.js &
There's also the jobs
command to see an indexed list of those backgrounded processes. And you can kill a backgrounded process by running kill %1
or kill %2
with the number being the index of the process.
Powerful solution (allows you to reconnect to the process if it is interactive):
screen
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.
You really should try to use screen
. It is a bit more complicated than just doing nohup long_running &
, but understanding screen once you never come back again.
Start your screen session at first:
user@host:~$ screen
Run anything you want:
wget http://mirror.yandex.ru/centos/4.6/isos/i386/CentOS-4.6-i386-binDVD.iso
Press ctrl+A and then d. Done. Your session keeps going on in background.
You can list all sessions by screen -ls
, and attach to some by screen -r 20673.pts-0.srv
command, where 0673.pts-0.srv is an entry list.