Linux下优雅的让程序后台运行

Linux下优雅的让程序后台运行

  假设在终端上启动运行了一个程序,跑了几天,如果不小心把terminal关了,那么程序就会终止,这是SIGHUP信号的原因,即使通过bg置为后台运行同样会如此,应该让程序成为一个daemon,步骤如下:


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.

技术分享


参考:
1.http://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。