6

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Mornor
  • 2,938
  • 7
  • 28
  • 61
  • 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 '16 at 12:24
  • "Which I run with python *script.sh**.", didn't you mean "Which I run with python script.py"? – Laurent LAPORTE Sep 14 '16 at 12:26
  • Yes, sorry with .py – Mornor Sep 14 '16 at 12:26
  • 3
    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... – Jon Clements Sep 14 '16 at 12:27
  • @PM2Ring I am running MacOS. – Mornor Sep 14 '16 at 12:28
  • This shoud work on Posix: `python -c "import sys; sys.exit(1)"; echo $?`=> 1 – Laurent LAPORTE Sep 14 '16 at 12:30
  • 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 '16 at 12:30

3 Answers3

8

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
ThomasGuenet
  • 183
  • 1
  • 5
  • 16
RedX
  • 14,219
  • 1
  • 51
  • 72
  • 1
    I dont quite see the idea of line 2 and 3 - why not just echo $? directly? – CutePoison Jan 15 '19 at 09: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 '19 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 '19 at 14:08
  • @Jakob Please post a new question so we can discuss it properly there. – RedX Jan 15 '19 at 15:08
2

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>.)

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
nigel222
  • 5,879
  • 1
  • 10
  • 18
-1

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)
Eric Leschinski
  • 135,913
  • 89
  • 401
  • 325