bobbyrne01
bobbyrne01

Reputation: 6763

Get exit code from subprocess.check_output

How can I get the exit code of subprocess.check_output?
I'm assuming the returncode should be 0 if a file is found matching my pattern and non-zero if no files are found matching the pattern?
If there is an exception, I'm recieving a non-zero returncode, as expected.

try:
        output = subprocess.check_output(["staf", "server.com", "PROCESS", "START", "SHELL", "COMMAND", "ls *heapdump*", "WAIT", "RETURNSTDOUT", "STDERRTOSTDOUT"])
        print result
except CalledProcessError as e:
        print(e.returncode)
        sys.exit(e.returncode)

Upvotes: 2

Views: 4247

Answers (1)

dano
dano

Reputation: 94961

Just as the documentation says:

If the return code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and any output in the output attribute.

So, if you don't end up in your except block, you can assume 0 was returned. You're already doing the right thing to handle non-zero return codes.

Upvotes: 4

Related Questions