CalledProcessError退出状态代码5 [英] CalledProcessError exit status code 5

查看:71
本文介绍了CalledProcessError退出状态代码5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理执行bash命令的简短python脚本.该程序工作了大约一个月.最近,我尝试通过以下命令运行脚本:

I have been working with a short python script that executes bash commands. The program worked fine for about a month. Recently, I tried running the script passing it this command:

my_launcher.py -c /path/to/config/file.json

(顺便说一句,顺便说一句,当在终端中输入命令时,我没有引起任何错误并且可以正常运行),并且收到以下消息:

(BTW, when the command is entered in terminal, I causes no error and runs fine) and I get the following message:

RuntimeError: Command '['bash', '-c', 'my_launcher.py -c /path/to/config/file.json']' returns non-zero exit status (code5)

在Google上搜索后,我发现了返回代码0、1和2的定义,但是没有发现代码5的定义.这是什么意思?如何解决?等等

After looking on Google, I found definitions for return codes 0, 1, and 2, but nothing for code 5. Wondering if any of you knows anything about it. What it means? How can it be resolved? etc.

这是导致错误的python代码:

This is the python code that causes the error:

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 时,这是提示信息:

When removing try/except, here is the taceback:

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

**正确的输出包含在e.output中.这意味着该命令已运行并返回正确的输出.我真的不知道为什么会收到此错误代码.

** The correct output is contained into e.output. Which means that the command is ran and returns correct output. I really don't know why I get this error code.

推荐答案

为进行记录,以下是运行.py文件的方法:

For the record, here's how you should run your .py file:

result = subprocess.check_output([
    sys.executable, 'my_launcher.py', '-c', path_to_json])

这是运行shell命令的方式:

And here is how you run shell commands:

result = subprocess.check_output(bash_command, shell=True)

对于您的问题-您可以从代码中删除 try/except ,以便我们可以看到完整的错误回溯吗?一些好的信息可能会隐藏在其中.

For your problem - can you remove the try/except from your code, so we can see the full error traceback? Some good information might be hidden in there.

这篇关于CalledProcessError退出状态代码5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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