M.Bris
M.Bris

Reputation: 35

CalledProcessError exit status code 5

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.

Upvotes: 1

Views: 7534

Answers (1)

nosklo
nosklo

Reputation: 222852

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.

Upvotes: 1

Related Questions