4

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.

1
  • 2
    That would depend on the cmd. Are you allowed to show the command being run ? Commented Feb 25, 2021 at 14:50

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The OS is Windows 10. The problem seemed to be related to the SAS program itself & not the subprocess.run() command. The information you provided was extremely helpful in understanding the issue was with the SAS program & not the subprocess.run() command.
You are welcome! I hope you can solve the SAS cmd. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.