python - 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
是空的。我该如何解决这个问题?
解决方案
我通过使用detach
来自http://inglorion.net/software/detach/的不同命令解决了这个问题
但我现在认为这是不恰当的。最好使用由您的 cron 作业脚本启动的 oneshot 服务,或者让您的 cron 条目导致启动 oneshot 服务。
这样就无需分离,因为进程不是您的脚本子进程,而是主管的子进程。任何支持启动正常关闭服务并且在退出时不重新启动它的 init 都可以使用。
推荐阅读
- python - 在 tkinter 中加载图像
- java - 如何通过 toString() 和 setter 和 getter 显示用户的多项选择?在爪哇
- python - Pandas:对 Pandas DataFrame 中的当前单元格和后续单元格求和
- scala - 我如何在 scala 中编写 AWS lamda 单元测试用例?
- python - AttributeError: 'builtin_function_or_method' 对象没有属性 'split' 3.7
- javascript - 如何获取卡片中单击的父元素的 id?
- haskell - 从列表中选择特定图片
- octave - 如何在 Octave 中无损地将双精度转换为字符串并返回
- javascript - 无法将数据保存在 chrome 扩展存储中
- html - CSS 动画在 IE11、Windows 7 和 Windows 10 中不起作用