[英]Have python script run in background of unix
我有一个python脚本,我想在UNIX服务器上的后台执行。 问题是我需要python脚本来等待上一步完成,然后再执行下一个任务,但是我希望我的工作在退出后继续运行。
我想我可以设置如下,但希望确认:
该脚本的摘录如下所示,其中命令2取决于命令1的输出,因为它在同一目录中输出已编辑的可执行文件。 我想指出的是,命令1和2没有包含nohup /&。
subprocess.call('unix command 1 with options', shell=True)
subprocess.call('unix command 2 with options', shell=True)
如果当我像这样启动我的python脚本时:
% nohup python python_script.py &
我的脚本会在后台运行,因为我没有明确将nohup /&放在脚本化的Unix命令中,而是在后台运行了python脚本?
是的,通过使用nohup(不挂机)运行python脚本,当网络断开时,脚本不会发生扭曲,尾随&符号将在后台运行脚本。
您仍然可以查看脚本的输出,nohup会将stdout传递给nohop.out文件。 您可以通过尾随该输出文件来实时查看输出:
$ tail -f nohop.out
关于nohup.out文件的快速说明...
nohup.out The output file of the nohup execution if
standard output is a terminal and if the
current directory is writable.
或在命令后加上&来作为守护程序运行python脚本并尾随日志。
$ nohup python python_script.py > my_output.log &
$ tail -f my_output.log
你可以使用nohup
chomd +x /path/to/script.py
nohup python /path/to/script.py &
要么
而不是关闭你的终端,使用logout
它时,你是不是SIGHUP logout
因此外壳不会发送SIGHUP到任何children.children的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.