Search code examples
node.jslinuxbackground-process

How to run Node.js as a background process and never die?


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.


Solution

  • 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.