Python子进程仅在cron中返回非零退出状态 [英] Python Subprocess returns non-zero exit status only in cron

查看:121
本文介绍了Python子进程仅在cron中返回非零退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,可以管理一系列 CasperJS 任务并处理结果。它可以从命令行正常运行,但是当我在cron中运行脚本时,出现错误:

I have a Python script that manages a series of CasperJS tasks and processes the result. It runs well from the command line, but when I run the script in cron, I get the error:

CalledProcessError: Command '['/path/to/casperjs', '/path/to/doSomething.js', 'args']' returned non-zero exit status 1

在Python中,我称为CasperJS:

In Python, I call CasperJS:

response = subprocess.check_output(['/path/to/casperjs', '/path/to/doSomething.js', 'args'], shell=True)

我也尝试过 shell = False Popen ,但是我得到了相同的结果结果。我还尝试将整个命令设置为字符串(而不是列表),但这也无济于事。

I have tried shell=False and Popen as well, but I get the same result. I also tried making the entire command a string (instead of list), but that didn't help either.

运行'/ path / to / casperjs /path/to/doSomething.js args'在shell中运行时返回退出代码0。

Running '/path/to/casperjs /path/to/doSomething.js args' returns exit code 0 when run in the shell.

我还添加了 PATH = / usr / bin:/ bin:/ sbin:/ usr / local / bin 到我的crontab都无济于事。 (如此问题中所建议。)

I have also added PATH=/usr/bin:/bin:/sbin:/usr/local/bin to my crontab to no avail. (As suggested in this question.)

有什么想法为什么只在cron中出现此错误?谢谢!!

Any ideas why I'm only getting this error in cron? Thanks!!

编辑:根据以下答案,设置 shell = False stderr = subprocess.STDOUT 使一切正常...

In accordance with the answer below, setting shell=False and stderr=subprocess.STDOUT made everything work...

推荐答案

除了stdout之外,您还应该尝试捕获stderr,以便您可以准确查明该程序失败的原因(假设它确实为您打印了一些错误)

You should try to capture stderr in addition to stdout so that you can find out exactly why the program is failing (assuming it does indeed print some errors for you)

cmd = ['/path/to/casperjs', '/path/to/doSomething.js', 'args']
response = subprocess.check_output(cmd, 
                shell=True,
                stderr=subprocess.STDOUT)

这篇关于Python子进程仅在cron中返回非零退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆