Mornor Mornor - 2 months ago 22
Bash Question

Get exit status code from Python

I have a python script which will do some actions, and depending on the result, will exit with a code 0 (all right) or 1 (not good).

I want to catch this result, store it into a variable and send it over UDP. This cannot be done inside the python script (requirement).

So let's say my script is something like:

import sys
# Do some stuff
sys.exit(0) # or sys.exit(1)


Which I run with
python script.py
.

How can I get this exit status code (0 or 1).

I try to echo
$errorlevel
, but it only print a blank line. Also try to use
exit(0)
instead of
sys.exit()
(see here) and some other unsuccessful attempts.

Answer

Since as per your answer you are using zsh

test.py

import os
os.exit(12)

in your console

python test.py
RC=$?
echo "Exit code $RC"
program_to_send $RC