假設在終端上啓動運行了一個程序,跑了幾天,如果不小心把terminal關了,那麼程序就會終止,這是SIGHUP信號的原因,即使通過bg置爲後臺運行同樣會如此,應該讓程序成爲一個daemon,步驟如下:
法1
1.Ctrl+z 暫停程序的運行,可以看到程序的作業號,假設爲1;2.bg %1 置爲後臺運行;
3. disown -h %1 使其不受終端關閉的影響。
=> The disown command on ksh shell causes the shell not to send a HUP signal to each given job, or all active jobs if job is omitted, when a login shell terminates.
=>The disown command on bash shell can either remove jobs or causes the shell not to send a HUP signal to each given job or all jobs.
./main.py 之前是終端上正常運行的普通程序。
按上面操作之後,通過 ps -ef | awk '$3 == 1' 命令可以看到程序變成了daemon.
法二:
nohup
your_command > /dev/null 2>&1 &
nohup :表示所屬終端關閉後,進程不會死掉;
> /dev/null
:標準輸出重定向到 /dev/null (a dummy device that does not record any output).2>&1
:標準出錯重定向到標準輸出,也到/dev/null最後的&
:後臺任務
1.http://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup
2.http://stackoverflow.com/questions/4797050/how-to-run-process-as-background-and-never-die