How to get the exit status set in a shell script in Python

import subprocess

result = subprocess.Popen("./compile_cmd.sh")
text = result.communicate()[0]
return_code = result.returncode

Taken from here: How to get exit code when using Python subprocess communicate method?


To complement cptPH's helpful answer with the recommended Python v3.5+ approach using subprocess.run():

import subprocess

# Invoke the shell script (without up-front shell involvement)
# and pass its output streams through.
# run()'s return value is an object with information about the completed process. 
completedProc = subprocess.run('./compile_cmd.sh')

# Print the exit code.
print(completedProc.returncode)

Tags:

Python

Unix

Shell