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. Commented Sep 14, 2016 at 12:24
  • "Which I run with python script.sh*.", didn't you mean "Which I run with python script.py"? Commented Sep 14, 2016 at 12:26
  • Yes, sorry with .py Commented 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... Commented 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? Commented 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
Sign up to request clarification or add additional context in comments.

4 Comments

I dont quite see the idea of line 2 and 3 - why not just echo $? directly?
@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.
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
@Jakob Please post a new question so we can discuss it properly there.
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 Comment

If one uses shell than last line should be change to [ $RC==0 ] && ....
-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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.