I got a message saying
script xyz.py returned exit code 0
. What does this mean?Exit Codes Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process. Other numbers can be used, but these are treated modulo 256, so exit -10 is equivalent to exit 246, and exit 257 is equivalent to exit 1.
What do the exit codes in Python mean? How many are there? Which ones are important?
T Zengerink3,36455 gold badges2525 silver badges3131 bronze badges
sundeepsundeep1,20855 gold badges1616 silver badges2121 bronze badges
12 Answers
What you're looking for in the script is calls to
sys.exit()
. The argument to that method is returned to the environment as the exit code.It's fairly likely that the script is never calling the exit method, and that 0 is the default exit code.
T Zengerink3,36455 gold badges2525 silver badges3131 bronze badges
Dave CostaDave Costa39.7k66 gold badges4949 silver badges6666 bronze badges
From the documentation for
sys.exit
:The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors.
One example where exit codes are used are in shell scripts. In bash you can check the special variable
$?
for the last exit status:Personally I try to use the exit codes I find in
cic/usr/include/asm-generic/errno.h
(on a Linux system), but I don't know if this is the right thing to do.6,02133 gold badges1919 silver badges3434 bronze badges
EigirEigir1,35911 gold badge1111 silver badges1818 bronze badges
There is an
errno
module that defines standard exit codes:For example, Permission denied is error code 13:
A-B-B25.6k66 gold badges7070 silver badges7474 bronze badges
OliOli10.1k66 gold badges2525 silver badges3535 bronze badges
For the record, you can use POSIX standard exit codes defined here.
Example:
laffustelaffuste10.2k44 gold badges6060 silver badges6767 bronze badges
Exit codes of 0 usually mean, 'nothing wrong here.' However if the programmer of the script didn't follow convention you may have to consult the source to see what it means. Usually a non-zero value is returned as an error code.
Sam CorderSam Corder4,62822 gold badges1818 silver badges3030 bronze badges
Operating system commands have exit codes. Look for linux exit codes to see some material on this. The shell uses the exit codes to decide if the program worked, had problems, or failed. There are some efforts to create standard (or at least commonly-used) exit codes. See this Advanced Shell Script posting.
S.LottS.Lott325k6969 gold badges448448 silver badges724724 bronze badges
If you would like to portably use the standard POSIX exit codes, see the exitstatus package on PyPI.
Install the package:
Use in your code:
phoenixphoenix1,92011 gold badge2020 silver badges2626 bronze badges
The exit codes only have meaning as assigned by the script author. The Unix tradition is that exit code 0 means 'success', anything else is failure. The only way to be sure what the exit codes for a given script mean is to examine the script itself.
Harper ShelbyHarper Shelby15.5k22 gold badges3737 silver badges5151 bronze badges
The answer is 'Depends on what exit code zero means'
However, in most cases, this means 'Everything is Ok'
I like POSIX:
So, on the shell, I would type:
python script.py && echo 'OK' || echo 'Not OK'
![Adgentns.pl exiting with status 2015 Adgentns.pl exiting with status 2015](/uploads/1/2/5/7/125783997/304636605.jpg)
If my python script calls
sys.exit(0)
the shell returns 'OK'If my python script calls
sys.exit(1)
(or any non-zero integer), the shell returns 'Not OK'It's your job to get clever with the shell, and read the documentation (or the source) for your script to see what the exit codes mean.
FlipMcFFlipMcF9,79711 gold badge2525 silver badges4343 bronze badges
I recommend a clean shutdown with the exit(0) builtin rather than using sys.exit()
I'm not sure if this is good advice.
The very documentation mentioned reads:
The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.
Then:
exit(code=None)
Objects that when printed, print a message like “Use quit() or Ctrl-D (i.e. EOF) to exit”, and when called, raise SystemExit with the specified exit code.
If we compare it to sys.exit() documentation, it's using the same mechanism which is raising a
ayaioSystemExit
exception.60k2020 gold badges135135 silver badges194194 bronze badges
weakcamelweakcamel
Exit codes in many programming languages are up to programmers. So you have to look at your program source code (or manual). Zero usually means 'everything went fine'.
DavideDavide11.6k1010 gold badges3939 silver badges6161 bronze badges
To allow exception handlers and other things execute I recommend a clean shutdown with the
vidstigevidstigeexit(0)
builtin rather than using sys.exit()
which terminates the process abruptly.8,66955 gold badges4747 silver badges9191 bronze badges