Python 子进程仅在 cron 中返回非零退出状态 [英] Python Subprocess returns non-zero exit status only in 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.
在 shell 中运行时,运行 '/path/to/casperjs/path/to/doSomething.js args'
会返回退出代码 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...
推荐答案
除了标准输出之外,您还应该尝试捕获标准错误,以便您可以准确找出程序失败的原因(假设它确实为您打印了一些错误)
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屋!