【发布时间】:2018-07-05 13:15:41
【问题描述】:
我一直在使用一个简短的 python 脚本来执行 bash 命令。该程序运行良好大约一个月。最近,我尝试运行脚本并传递以下命令:
my_launcher.py -c /path/to/config/file.json
(顺便说一句,当在终端中输入命令时,我不会导致任何错误并且运行正常)并且我收到以下消息:
RuntimeError: Command '['bash', '-c', 'my_launcher.py -c /path/to/config/file.json']' returns non-zero exit status (code5)
在 Google 上查找后,我找到了返回代码 0、1 和 2 的定义,但没有找到代码 5 的定义。想知道你们中是否有人对此有所了解。这是什么意思?如何解决?等等。
这是导致错误的python代码:
try :
#check_output produces byte string
#raises exception if command returns a non-zero exit status (error occurs during processing of command)
string_of_text_rc = subprocess.check_output(['bash', '-c', bashCommand])
except subprocess.CalledProcessError as e:
raise RuntimeError("Command '{}' returns non-zero exit status (code{})".format(e.cmd, e.returncode))
删除try/except 时,这里是回退:
Traceback (most recent call last):
File "bash_cmd.py", line 27, in <module>
run_cmd('my_launcher.py -c /path/to/config/file.json')
File "bash_cmd.py", line 17, in run_cmd
string_of_text_rc = subprocess.check_output(['bash', '-c', bashCommand])
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['bash', '-c', 'my_launcher.py -c /path/to/config/file.json']' returned non-zero exit status 5
**EDIT:正确的输出包含在 e.output 中。这意味着该命令已运行并返回正确的输出。我真的不知道为什么我会收到这个错误代码。
【问题讨论】:
标签: python bash subprocess runtime-error exit-code