gpt4 book ai didi

python - 通过 cron 运行时获取 subprocess.Popen stdout

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:34 24 4
gpt4 key购买 nike

我想获取服务状态,如果未启动,则通过电子邮件发送状态 (stdout)。
此脚本由 cron 计划每小时运行一次。
手动运行时,以下工作正常:

def is_service_running(name):
with open(os.devnull, 'wb') as hide_output:
proc = subprocess.Popen(['service', name, 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output = proc.stdout.read()
exit_code = proc.wait()
return exit_code == 0, output

但是在cron运行的时候。 输出为空。
如何在 cron 运行时捕获 stdout?谢谢

最佳答案

问题不是 cron,而是 shell=True
显然,当使用 shell=True 时,popen 需要单个字符串而不是列表。
因此,当我将电话更新为:

proc = subprocess.Popen(['service ' + name + ' status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

一切正常。

关于python - 通过 cron 运行时获取 subprocess.Popen stdout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com