Exit Codes: Difference between revisions

From Leo's Notes
This page was last edited on 28 April 2021, at 23:26.
No edit summary
No edit summary
Line 8: Line 8:
|-
|-
|1
|1
|General Error
|General Error, typical catch-all value
|-
|-
|2
|2
|Misuse of shell builtins, keywords, or functions
|Misuse of shell builtins, keywords, or functions
|-
|6
|No such device or address. Eg. curl on an invalid host.
|-
|124
|Command timed out (coreutils/timeout)
|-
|125
|Command failure (coreutils/timeout)
|-
|-
|126
|126
|Cannot Execute
|Command found, but cannot be executed
|-
|-
|127
|127
|Command Not Found. Path or typo issue.
|Command not found. Possible path or typo issue.
|-
|128
|Invalid argument to exit. Eg. <code>exit 1.1</code> would result in 128.
|-
|-
|128+n
|128+n
Line 24: Line 36:
|-
|-
|130
|130
|Program Interrupted (SIGINT, 2), such as termination from Ctrl-C
|Program Interrupted (SIGINT, 128+2), such as termination from Ctrl-C
|-
|-
|134
|134
|Program Abort (SIGABRT, 6)
|Program Abort (SIGABRT, 128+6)
|-
|-
|136
|136
|Erroneous Arithmetic Operation (SIGFPE, 8), such as division by zero
|Erroneous Arithmetic Operation (SIGFPE, 128+8), such as division by zero
|-
|137
|Killed. (SIGKILL, 128+9)
|-
|-
|139
|139
Line 45: Line 60:
|-
|-
|256+
|256+
|All higher exit codes should be reduced down to the 0-255 range. More information below.
|All higher exit codes should be reduced down to the 0-255 range. More information below. You typically either modulo the code by 256 or take the high 8-bit values.
Eg. Code 3809 % 256 results in 225
Eg. Code <code>3809 % 256</code> results in 225, or code <code>32512 & 0xff00 >> 8</code> results in 127.
|}
|}


== Parsing high exit code values ==
==Parsing high exit code values==
At least on Linux (and whatever is reported by Slurm), the exit code that's stored is an 2 byte value which can be parsed as:
At least on Linux (and whatever is reported by Slurm), the exit code that's stored is an 2 byte value which can be parsed as:
{| class="wikitable"
{| class="wikitable"
Line 69: Line 84:


*https://tldp.org/LDP/abs/html/exitcodes.html
*https://tldp.org/LDP/abs/html/exitcodes.html
*Coreutils / Timeout: <nowiki>https://www.gnu.org/software/coreutils/manual/html_node/timeout-invocation.html</nowiki>

Revision as of 23:26, 28 April 2021

Here are some common exit codes that may be returned by a program or script.

Status Description
0 Successful Termination
1 General Error, typical catch-all value
2 Misuse of shell builtins, keywords, or functions
6 No such device or address. Eg. curl on an invalid host.
124 Command timed out (coreutils/timeout)
125 Command failure (coreutils/timeout)
126 Command found, but cannot be executed
127 Command not found. Possible path or typo issue.
128 Invalid argument to exit. Eg. exit 1.1 would result in 128.
128+n Fatal error signal n, where n is the signal number as defined in:

https://man7.org/linux/man-pages/man7/signal.7.html

130 Program Interrupted (SIGINT, 128+2), such as termination from Ctrl-C
134 Program Abort (SIGABRT, 128+6)
136 Erroneous Arithmetic Operation (SIGFPE, 128+8), such as division by zero
137 Killed. (SIGKILL, 128+9)
139 Segmentation Fault (SIGSEGV, 11)
160+n The largest signal value is 31. As a result, all values after 128+31 can be interpreted as negative return codes by treating the value as a 1 byte integer.

For example, 255 = -1, 254 = -2, 240 = -16, 225 = -31, and so forth

A simple way to 'convert' it is to just subtract the value from 256.

255 Maximum exit code value. Obtained when the program calls exit -1.
256+ All higher exit codes should be reduced down to the 0-255 range. More information below. You typically either modulo the code by 256 or take the high 8-bit values.

Eg. Code 3809 % 256 results in 225, or code 32512 & 0xff00 >> 8 results in 127.

Parsing high exit code values

At least on Linux (and whatever is reported by Slurm), the exit code that's stored is an 2 byte value which can be parsed as:

Bits Description
high 8 bits The actual exit code
7 bit if a core dump was produced
lower 7 bits signal number that killed the process

For example, the exit code 32512 has an exit code of 127 (command not found).

See Also