CalledProcessError exit status code 5

爷,独闯天下 提交于 2019-12-24 14:07:12

问题


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)

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.

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))

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

**EDIT: 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.


回答1:


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])

And here is how you run shell commands:

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

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.



来源:https://stackoverflow.com/questions/51192313/calledprocesserror-exit-status-code-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!