0

I am using Python 3.6.9 to call the following bash script:

run_cmds.sh:

ls dir_doesnt_exist | tee log

Here is the Python code:

import subprocess 
from subprocess import PIPE

cmd = subprocess.run(['bash','run_cmds.sh'], stdout=PIPE, stderr=PIPE)

print(cmd.stderr.decode())
print("The returned code is:", cmd.returncode)

Running the Python code, I get the following:

ls: cannot access 'dir_doesnt_exist': No such file or directory

The returned code is: 0

As you can see, subprocess captures the standard error output but returncode is 0.

What is wrong with my Python script?

I appreciate any help.

1 Answer 1

1

The return code IS 0. The return code is the return code of the last command, even when using tee (unless the pipefail variable is set). You can see it for yourself on the command line:

$ ls dir_doesnt_exist | tee log
ls: dir_doesnt_exist: No such file or directory

$ echo $?
0

However, if you remove the pipe |, you will get a non-zero exit code

$ ls dir_doesnt_exist
ls: dir_doesnt_exist: No such file or directory

$ echo $?
1

So, when using tee, you have to check the $PIPETSTATUS variable instead of the regular exit code

$ ls dir_doesnt_exist | tee log
ls: dir_doesnt_exist: No such file or directory

$ echo ${PIPESTATUS[0]}
1

Try making your python code like this

import subprocess 
from subprocess import PIPE

cmd = subprocess.run(['bash','run_cmds.sh; exit ${PIPESTATUS[0]}'], stdout=PIPE, stderr=PIPE)

print(cmd.stderr.decode())
print("The returned code is:", cmd.returncode)
Sign up to request clarification or add additional context in comments.

Comments

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.