CalledProcessError exit status code 5 - 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)
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.

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.

Related

subprocess.CalledProcessError: returned non-zero exit status 1, while os.system does not raise any error

Given the following command:
newman run tests.postman_collection.json -e environment.json --reporters testrail,json,html
Raises:
RuntimeError: command 'newman run tests.postman_collection.json -e environment.json --reporters testrail,json,html
' return with error (code 1): b'\nhttps://host.testrail.io/index.php?/runs/view/1234\n'
Py code that executes the command:
try:
newmanCLI_output = subprocess.check_output(npmCLi, shell=True).decode().strip()
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
And yes I do use the check_output return.
The output is a url to test rail reports
That's a misfeature of os.system; it returns the exit code so you can examine it, but doesn't raise an error if something fails.
The check in subprocess.check_output means check that the command succeeded, or raise an exception otherwise. This is generally a good thing, as you don't want processes to die underneath you without a warning.
But you can work around it with subprocess.run if you want to disable it;
import shlex
result = subprocess.run(shlex.split(npmCLi), text=True, capture_output=True)
newmanCLI_output = result.stdout
The switch to avoid shell=True and use shlex.split to parse the string instead is not crucial, but hopefully demonstrates how to do these things properly.
You should still understand why exactly your command fails, and whether it is safe to ignore the failure.

check_output doesn't works in python 3.6 while subprocess works fine

i am trying to get the output of a command in my python program by using "check_output" method. but i'm getting this error:
out = check_output(command5 , shell=True)
File "/usr/lib64/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/usr/lib64/python3.6/subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_rht-ccp --results-arf arf.xml /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml' returned non-zero exit status 2.
this is the part of my program that is related:
command4 = "oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_rht-ccp --results-arf arf.xml /usr/share/xml/scap/ssg/content/ssg-centos7-ds.xml"
out = check_output(command4 , shell=True)
I am sure that the command is alright because I get the results when I write:
subprocess.call(command5,shell=True)
I am using python 3.6, and work in centos 7.
any idea why the check_output can not get the result?
This is entirely normal, because the command you ran produced a non-zero exit code. It means that the command you ran is signalling that something may be wrong.
See the subprocess.check_output() documentation:
If the return code was non-zero it raises a CalledProcessError.
and
This is equivalent to:
run(..., check=True, stdout=PIPE).stdout
where the check=True flag tells run() to raise an exception when return_value is not 0:
If check is true, and the process exits with a non-zero exit code, a CalledProcessError exception will be raised.
The other function you used, subprocess.call(), does not set check=True:
Run the command described by args. Wait for command to complete, then return the returncode attribute.
This is equivalent to:
run(...).returncode
So either don't use check_output(), or catch the exception thrown, or fix the command you are running. That call() worked is no indication that the process actually produced a successful result.
For example, you could use subprocess.run() directly:
proc = subprocess.run(
command5, shell=True, text=True
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if proc.returncode:
print(f'Issue reported, exit code {proc.returncode}, stderr:')
print(proc.stderr)
else:
print(proc.stdout)

Kill a python script from another script - CalledProcessError

I want to kill a python script that runs on my system from another python script.
I followed this answer and tweaked the code a bit, but got an error:
Traceback (most recent call last): File "/home/pi/base.py", line 13, in <module>
check_call(["pkill", "-9", "-f", script])
File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
CalledProcessError: Command '['pkill', '-9', '-f', '/home/pi/MotionDetector.py']' returned non-zero exit status 1
Code:
from subprocess import check_call
import sys
import time
script = '/home/pi/MotionDetector.py'
check_call(["pkill", "-9", "-f", script])
This means the pkill call has failed. Two possible reasons that come to mind:
it did not actually match any process. pkill would in this case not generate any output and return 1. You can verify this by trying to run pgrep instead of pkill and see what did it return on stdout (should be one or more lines with a PID in case of a match) and/or whether it also returned a non-zero status.
it did match, but user under which the pkill has been executed cannot kill the process(s) matched. In that case pkill should generate output on stderr similar to: pkill: killing pid 3244 failed: Operation not permitted
From the pkill(1) man page:
EXIT STATUS
...
1 No processes matched or none of them could be signalled.
...
It turns out it was just a bug.
The solution was simple, I copied my script to a new file, deleted the old one and it worked, simple as that.

Subprocess call is not working in python but the command is working in terminal

When i run ./fasttext from terminal it runs well. But when i try it with subprocess.check_output('./fasttext') it is giving the error as.
Error
CalledProcessError
Traceback (most recent call last)
<ipython-input-11-a048dbef034f> in <module>()
1 from subprocess import check_output
2 from subprocess import call
----> 3 a1 = check_output('./fasttext')
4
5 #a = check_output('./fasttext print-sentence-vectors ~/Vaiju/Project/all code and data/My Code/TrainedModels/wiki_unigrams.bin < 1fasttext.test.txt')
/home/vaijenath/anaconda2/lib/python2.7/subprocess.pyc in check_output(*popenargs, **kwargs)
571 if cmd is None:
572 cmd = popenargs[0]
--> 573 raise CalledProcessError(retcode, cmd, output=output)
574 return output
575
CalledProcessError: Command './fasttext' returned non-zero exit status 1
I am in the same directory as fasttext is present in notebook.
Resolved. The problem is solved by using shell=True argument in call.
check_output only throws an error if the exit status isn't 0. The command you are trying to run ./fasttext exits with 1 because you didn't follow the correct usage pattern. In the terminal it gives you some documentation but still exits with 1, if you don't believe me (and you shouldn't ;)) run ./fasttext; echo $? in the terminal and you'll see the exit status.
So simply use something like:
a1 = check_output(['./fasttext', 'predict', <model>, <out>])
Notes :
Check output returns a byte. You might be interested in .decode("utf-8") in order to .split("\n")
Facebook now has a very simple wrapper for python
Your real use case should look like this:
import subprocess
import os.path
a = subprocess.check_output([
'./fasttext',
'print-sentence-vectors',
os.path.expanduser('~/Vaiju/Project/all code and data/My Code/TrainedModels/wiki_unigrams.bin'),
], stdin=open('1fasttext.test.txt', 'r'))
Note that each argument is passed as a separate list member; redirections are performed via Python (in this case, using stdin= instead of a < redirection), and replacing expansions like ~ has to be done by Python (as, here, with os.path.expanduser()).

python - Error when trying to receive output from check_output

My code is supposed to track down a packet sniffed with scapy and check which program sent/received the packet, put "Unknown" if the program isn't found
Code :
source_software = check_output(["netstat", "-nb"], shell = True).decode()
source_software = source_software[source_software.find(str(packet_dictionary["Port"])) : ]
source_software = source_software[source_software.find("\n") + 2 : source_software.find("]")]
if "Can not" not in source_software and len(source_software) > MINIMUM_LENGTH:
packet_dictionary["Software"] = source_software
else:
packet_dictionary["Software"] = "Unknown"
Errors :
File "Client.py", line 44, in add_to_list
source_software = check_output(["netstat", "-nb"], shell = True).decode()
File "C:\Python36\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Python36\lib\subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['netstat', '-nb']' returned non-zero
exit status 1.
it could be python doesn't have permission to run netstat or any else, but you can debug it with the following command
source_software = check_output("netstat -nb; exit 0", stderr=subprocess.STDOUT, shell=True).decode()
print source_software
As you can see in the bottom line of the exceptions, the problem is that:
Command '['netstat', '-nb']' returned non-zero exit status 1.
...which means that is it not about check_output itself, rather it is just reporting back that the command you attempted to run using it, failed and exited with code 1.
You may want to run the command in the shell and check if it work as expected.

Categories

Resources