简体   繁体   English

python:运行'nohup':如何摆脱消息?

[英]python: running 'nohup': how to get rid of message?

I have a variable list of programs that I want to kick off from a cron job.我有一个可变的程序列表,我想从 cron 作业开始。 The solution I have settled on at least for now is to write the actual cron job in python, then run through the list, starting each program with:我至少现在确定的解决方案是在 python 中编写实际的 cron 作业,然后遍历列表,启动每个程序:

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

The problem with this is that it creates a nohup.out file - the same one for each process, it seems.这样做的问题是它创建了一个 nohup.out 文件——似乎每个进程都使用相同的文件。 If I did this same thing from the command line, it might look like:如果我从命令行做同样的事情,它可能看起来像:

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

This works fine, except I get a message from nohup when I run it:这工作正常,除非我在运行时从 nohup 收到一条消息:

nohup: ignoring input and redirecting stderr to stdout

I have tried to redirect stderr to /dev/null, but the result is that the_command.log is empty.我试图将 stderr 重定向到 /dev/null,但结果是the_command.log为空。 How can I solve this?我该如何解决这个问题?

I solved this by using a different command detach from http://inglorion.net/software/detach/我通过使用不同的命令从http://inglorion.net/software/detach/ detach解决了这个问题

But I now consider this to be improper.但我现在认为这是不合适的。 It would be better to use oneshot services started by your cron job script or make your cron entry cause a oneshot service to be started.最好使用由您的 cron 作业脚本启动的 oneshot 服务,或者让您的 cron 条目导致启动 oneshot 服务。

With this there would be no need to detach as the processes aren't your scripts children rather they are children of the supervisor.有了这个,就不需要分离了,因为这些进程不是你的脚本子进程,而是主管的子进程。 Any init that supports starting a normally down service and does not restart it when it exits can be used.任何支持启动一个正常关闭的服务并且在它退出时不重新启动它的 init 都可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM