I am currently running subprocess with the specified arguments I have below. Everything appears to run fine. The generated output is as expected & the log generated shows no errors.
exit_code = subprocess.run([cmd, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11], capture_output=True)
However I get a return code / exit code of 2.
The stderr & stdout show the following when I print the exit_code above.
returncode=2, stderr=b'', stdout=b''
Why would I get expected output & no errors in log but still get an exit code of 2? Is there a way to figure out why the exit code is returning 2? What does b'' mean for stderr & stdout?
I am running a SAS program with subprocess.run. This only occurs for some SAS programs. The one's that it does occur for seem to generate pdf files. Also, if I use the exact same arguments specified in subprocess.run into a .bat file it runs fine with an exit code of 0.
First of all it would be nice to know what OS you are using. Basically the stderr and stdout are file descriptors (1 and 2 by default.)
In your case the subcommand writes nothing to 1 and 2 file descriptors (so to stderr and stdout) and of course the return code is 2.
If you run the following code:
import subprocess
result = subprocess.run(["echo", "test"])
print(result.returncode, result.stdout, result.stderr)
You get:
>>> python3 test.py
test
0 None None
It means the echo runs correctly and writes the test to the STDOUT but the result.stdout doesn't contain it. This happens but you don't redirect the STDOUT of the command to a PIPE.
If you change the code to:
import subprocess
result = subprocess.run(["echo", "test"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.returncode, result.stdout, result.stderr)
You will get:
>>> python3 test.py
0 b'test\n' b''
So now the result.stdout contains the STDOT of the called command in bytes. It is True for STDERR.
If a set the shell parameter to True, I can run invalid (not existed) command as you can see below:
import subprocess
result = subprocess.run(["invalid_cmd"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print(result.returncode, result.stdout, result.stderr)
In this case the output is:
>>> python3 test.py
127 b'' b'/bin/sh: invalid_cmd: command not found\n'
It means the return code is 127 and the STDOUT is empty but the STDERR contains /bin/sh: invalid_cmd: command not found\n message in bytes.
If you do the above things but you don't see any output then your called command doesn't write the STDOUT or STDERR so you should look into the called command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With