How to get the exit status set in a shell script in Python
Question:
I want to get the exit status set in a shell script which has been called from Python.
The code is as below
Python script
result = os.system("./compile_cmd.sh")
print result
File compile_cmd.sh
javac @source.txt
# I do some code here to get the number of compilation errors
if [$error1 -e 0 ]
then
echo "n********** Java compilation successful **********"
exit 0
else
echo "n** Java compilation error in file ** File not checked in to CVS **"
exit 1
fi
I am running this code, but no matter the what exit status I am returning, I am getting result var as 0 (I think it’s returning whether the shell script ran successfully or not).
How can I fetch the exit status that I am setting in the shell script in the Python script?
Answers:
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)
import subprocess
proc = subprocess.Popen("Main.exe",stdout=subprocess.PIPE,creationflags=subprocess.DETACHED_PROCESS)
result,err = proc.communicate()
exit_code = proc.wait()
print(exit_code)
print(result,err)
In subprocess.Popen -> creation flag is used for creating the process in detached mode
if you don’t wnat in detached more just remove that part.
subprocess.DETACHED_PROCESS -> run the process outside of the python process
with proc.communicate() -> you can get he output and the errors from that process
proc.wait() will wait for the process to finish and gives the exit code of the program.
Note: any commands between subprocess.popen() and proc.wait() will execute as usual at the wait call it will not execute further before the subprocess is finished.
I want to get the exit status set in a shell script which has been called from Python.
The code is as below
Python script
result = os.system("./compile_cmd.sh")
print result
File compile_cmd.sh
javac @source.txt
# I do some code here to get the number of compilation errors
if [$error1 -e 0 ]
then
echo "n********** Java compilation successful **********"
exit 0
else
echo "n** Java compilation error in file ** File not checked in to CVS **"
exit 1
fi
I am running this code, but no matter the what exit status I am returning, I am getting result var as 0 (I think it’s returning whether the shell script ran successfully or not).
How can I fetch the exit status that I am setting in the shell script in the Python script?
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)
import subprocess
proc = subprocess.Popen("Main.exe",stdout=subprocess.PIPE,creationflags=subprocess.DETACHED_PROCESS)
result,err = proc.communicate()
exit_code = proc.wait()
print(exit_code)
print(result,err)
In subprocess.Popen -> creation flag is used for creating the process in detached mode
if you don’t wnat in detached more just remove that part.
subprocess.DETACHED_PROCESS -> run the process outside of the python process
with proc.communicate() -> you can get he output and the errors from that process
proc.wait() will wait for the process to finish and gives the exit code of the program.
Note: any commands between subprocess.popen() and proc.wait() will execute as usual at the wait call it will not execute further before the subprocess is finished.