首页 > 解决方案 > python:运行'nohup':如何摆脱消息?

问题描述

我有一个可变的程序列表,我想从 cron 作业开始。我至少现在确定的解决方案是在 python 中编写实际的 cron 作业,然后运行列表,每个程序都使用:

outf=open('the_command.log','w')
subprocess.Popen(['nohup','the_command', ...],stdout=outf)
outf.close()

这样做的问题是它创建了一个 nohup.out 文件——似乎每个进程都使用同一个文件。如果我从命令行做同样的事情,它可能看起来像:

$ nohup the_command ... > the_command.log 2>&1 

这工作正常,除了我在运行它时收到来自 nohup 的消息:

nohup: ignoring input and redirecting stderr to stdout

我试图将 stderr 重定向到 /dev/null,但结果the_command.log是空的。我该如何解决这个问题?

标签: pythonnohup

解决方案


我通过使用detach来自http://inglorion.net/software/detach/的不同命令解决了这个问题

但我现在认为这是不恰当的。最好使用由您的 cron 作业脚本启动的 oneshot 服务,或者让您的 cron 条目导致启动 oneshot 服务。

这样就无需分离,因为进程不是您的脚本子进程,而是主管的子进程。任何支持启动正常关闭服务并且在退出时不重新启动它的 init 都可以使用。


推荐阅读