Definition
The exit status of a process, a running program, is the small integer it reports to its parent when it ends, through the exit function or a return from its main function. The parent receives only the lowest 8 bits, a value from 0 to 255. By convention 0 reports success and other values report failure or another outcome; each program defines its own values. A process killed by a signal, a kernel notice that can end it, has no exit status; the parent learns the signal number instead.
The idea
A test script runs a hypothetical command submit job.conf and prints PASS when the command returns 0. The command returns 0 as soon as the job has been queued. Two minutes later the job fails, and nothing reports it. In another script, bench | tee log.txt returns 0 although bench crashed, because a pipeline reports the status of its last command, here tee, which copied the output faithfully.
An exit status is a one-byte message from one program about what that program did, in that program’s own terms. A courier’s receipt is a fair picture: it says that the parcel was handed over, not that it arrived intact or that its contents were right. The picture stops being accurate in one respect: a program’s documentation can make its status mean more, as a test runner that returns 0 only when every test passed does, and a program can return a nonzero status for an outcome that is not an error.
How it works
The status travels in four steps, and each step can change what it says.
The program ends. It calls
exit(v)or returns frommain. The C standard fixes only that 0 andEXIT_SUCCESSmean successful termination and thatEXIT_FAILUREmeans unsuccessful termination.The kernel keeps 8 bits. The parent can see only modulo 256, a value from 0 to 255.
The parent decodes. The parent’s
waitorwaitpidcall returns a wait status that encodes how the child ended. POSIX (Portable Operating System Interface), the standard behind Unix-like systems, defines macros to read it:WIFEXITEDtells whether the child exited normally, and thenWEXITSTATUSgives the 8-bit exit status;WIFSIGNALEDtells whether a signal killed the child, and thenWTERMSIGgives the signal number, with no exit status at all.Tools translate. Each tool that reports the outcome maps it to its own convention.
Common translations are these.
A shell, the program that reads and runs typed commands, keeps the status of the last command in the special parameter
$?. For a command killed by signal , POSIX shells report a value above 128; bash, the most common Linux shell, and most others report . Status 127 means that the command was not found, and 126 that it was found but could not be run.A pipeline such as
a | b, which feeds the output ofatob, reports the status of its last command only. The optionset -o pipefail, offered by bash and other shells, makes it report the last nonzero status instead, and bash also keeps every status in the array §14.5PIPESTATUS.A command started in the background, with
&after it, gives status 0 at once; its real status is known only after the shell waits for it.Python’s §12.23
subprocessmodule reports for a child killed by signal .The
timeoutcommand found on Linux systems exits with 124 when it had to stop the command for running too long.ssh, which runs a command on another computer, returns that command’s status, or 255 whensshitself failed.systemd, the Linux service manager, counts 0, the values listed in
SuccessExitStatus=and four termination signals as success for a service.
Programs also define nonzero values that are not errors. grep, which searches text, exits with 0 when it found a matching line, 1 when it found none and 2 on an error. diff, which compares files, exits with 0 when they are the same, 1 when they differ and 2 on trouble. A test that expects a program to refuse an input expects a nonzero status. A submission command exits with 0 when its request was accepted: systemd-run, for example, returns once the command it started has begun to run, not when that command ends.
Worked example
Truncation. A program calls
exit(256). The parent sees , which reads as success.exit(300)arrives as , andexit(-1)as .Signals. A program is killed by
SIGKILL, signal 9. The raw wait status hasWIFSIGNALEDtrue andWTERMSIGequal to 9; bash shows ; Python’ssubprocessshows . A program that calledexit(137)also shows 137 in bash, so the shell’s value alone cannot tell the two apart.Pipeline. In
bench | tee log.txt,benchexits with 7 andteewith 0. The pipeline reports 0; withpipefailit reports 7;PIPESTATUSholds 7 and 0.Submission. A harness runs
systemd-run bench, which exits with 0 at once;benchexits with 7 a minute later. The 0 means submitted, and the 7 is in the service’s record. A rule that accepts the run needs the 7, an output check and an eligibility check; the 0 contributes only the fact that the start succeeded.
Stated precisely
POSIX states the encoding. If a process ended by calling exit with the integer , its parent’s wait status satisfies WIFEXITED(s), and where is the remainder of divided by 256, taken in the range 0 to 255, so that . If the process was ended by signal number , WIFSIGNALED(s) holds, WTERMSIG(s) equals , and no exit status exists.
The meaning of each value is set by the program’s documentation. The C standard fixes only two meanings: 0 or EXIT_SUCCESS means successful termination, and EXIT_FAILURE means unsuccessful termination, each in a form the system defines. A run can be accepted by a rule of this form:
every process whose result matters ended normally with its expected status, which a test of a refusal sets to a nonzero value;
the requested work was completed, not only submitted;
the required outputs exist and pass the test oracle §10.5, the check that decides whether output is correct;
the run meets the rules of evidence eligibility §13.14, which say which runs may count.
An exit status can satisfy the first condition at most.
Limits and common mistakes
Zero means what the program’s documentation says, nothing more: often that the program detected no error of its own, sometimes only that a request was accepted.
A wrapper can replace a worker’s status with its own. A script’s status is the status of its last command, so a script whose last line prints a message returns 0 after a failed step, and so does a command followed by || true. A wrapper should pass on the worker’s status or record both.
A status covers one process. Processes it left running, and work it handed to others, are not described; process closure §13.23 is a separate check.
Many signal numbers differ between systems; POSIX fixes only a few, such as 9 for SIGKILL and 15 for SIGTERM. Record signal names, such as SIGTERM, rather than numbers.
The 8-bit range and the shell’s rule make values ambiguous: 137 can be an exit or a kill, and 256 becomes 0.
A timeout §9.34 tool’s own status, such as 124, replaces the command’s status. Record that the run was stopped.
Status 0 is no evidence that output is complete or valid.
Questions answered
Does zero mean submitted, completed, validated, or merely no local error?
Zero means only what the documentation of the program that exited says it means; it can mean any of the four, and without that documentation it means at most that this one program reports no error of its own. An exit status is the small integer, 0 to 255, that a process reports to its parent when it ends. For a submission command, zero means that the request was accepted: submitted, not completed. For a program that does the work itself, zero usually means that the work ran to its end without an error the program detected: completed locally. Zero means validated only for a program whose job is validation, such as a test runner whose documentation says that it returns 0 only when every check passed. Zero never covers processes that the program left running or work it handed to other programs.
To accept a run, record the raw status of every process whose result matters, including whether a signal killed it, then check separately that the work completed, that the outputs are present and correct, and that the run meets the eligibility rules.
Related entries: subprocess (§12.40), Evidence eligibility (§13.14), Process closure (§13.23), Test oracle (§10.5).