How to run process as background 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 that I can keep the process alive even 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):

nohup node server.js &

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.


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, eg: 2>/tmp/myLog
  • & at the end means: run this command as a background task.

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

    链接地址: http://www.djcxy.com/p/90028.html

    上一篇: 如何在启动时运行一个shell脚本

    下一篇: 如何以后台运行进程并永不死亡?