9

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 prints a blank line. I also try to use exit(0) instead of sys.exit() (see here) and some other unsuccessful attempts.

7
  • 7
    sys.exit(0) is the correct way to send the return code to your shell. But you didn't say which OS or shell you're using. In bash & similar shells, $? has the exit code of the last command.
    – PM 2Ring
    Sep 14, 2016 at 12:24
  • "Which I run with python script.sh*.", didn't you mean "Which I run with python script.py"? Sep 14, 2016 at 12:26
  • Yes, sorry with .py
    – Mornor
    Sep 14, 2016 at 12:26
  • 4
    Is this actually a question about Python? As far as I can tell you're asking how to capture the exit code of a process (it's Python in this case) - but it could be any process. As @PM2Ring mentions - this is dependant upon your shell/environment etc... Sep 14, 2016 at 12:27
  • 1
    This is not a question on python per se. it is a question about your shell. Which one do you use? Bash, sh, fish, ...? in bash you capture the last exit code with $?. How are you sending the captured variables?
    – RedX
    Sep 14, 2016 at 12:30

3 Answers 3

9

Since as per your answer you are using zsh

test.py

import sys
sys.exit(12)

in your console

python test.py
RC=$?
echo "Exit code $RC"
program_to_send $RC
4
  • 1
    I dont quite see the idea of line 2 and 3 - why not just echo $? directly?
    – CutePoison
    Jan 15, 2019 at 9:43
  • @Jakob echo would set it's own exit code and the second call would use that instead of the original one. So to make both consistent I store the return and use it in both places.
    – RedX
    Jan 15, 2019 at 13:25
  • 1
    when I do it (without the RC=$?), it returns the correct values - 0 if everything is fine, but when I force an error in the .py file, it returns 1
    – CutePoison
    Jan 15, 2019 at 14:08
  • @Jakob Please post a new question so we can discuss it properly there.
    – RedX
    Jan 15, 2019 at 15:08
3

It's a script question, not a Python question, but in the Bash shell you use $? and since any command changes $?, it's usually a good idea to save a copy.

python script.py
RC=$?

echo $RC
[ $RC == 0 ] && echo Success || echo Failed
# Replace above lines by whatever you need to send $RC via UDP ...

If it's a Windows CMD script question, you should probably repost with the appropriate tags. (I don't do Windows any more, thank <deity>.)

1
  • If one uses shell than last line should be change to [ $RC==0 ] && .... Oct 29, 2018 at 13:05
-2

This should be done the right way by catching exceptions and using logger.

import logging, time

logging.basicConfig(level=logging.DEBUG, filename='/path/to/file/errorlog.log')

try:
    #MySQL Connection
except MySQLdb.Error as e:
    errortime = time.strftime('%Y-%m-%dT%H:%M:%S')
    logging.exception(errortime)
    raise os._exit(0)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.