Section 13.24

Exit status

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.

  1. The program ends. It calls exit(v) or returns vv from main. The C standard fixes only that 0 and EXIT_SUCCESS mean successful termination and that EXIT_FAILURE means unsuccessful termination.

  2. The kernel keeps 8 bits. The parent can see only vv modulo 256, a value from 0 to 255.

  3. The parent decodes. The parent’s wait or waitpid call 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: WIFEXITED tells whether the child exited normally, and then WEXITSTATUS gives the 8-bit exit status; WIFSIGNALED tells whether a signal killed the child, and then WTERMSIG gives the signal number, with no exit status at all.

  4. Tools translate. Each tool that reports the outcome maps it to its own convention.

Common translations are these.

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

  1. Truncation. A program calls exit(256). The parent sees 256mod256=0256 \bmod 256 = 0, which reads as success. exit(300) arrives as 300−256=44300 - 256 = 44, and exit(-1) as −1+256=255-1 + 256 = 255.

  2. Signals. A program is killed by SIGKILL, signal 9. The raw wait status has WIFSIGNALED true and WTERMSIG equal to 9; bash shows 128+9=137128 + 9 = 137; Python’s subprocess shows −9-9. A program that called exit(137) also shows 137 in bash, so the shell’s value alone cannot tell the two apart.

  3. Pipeline. In bench | tee log.txt, bench exits with 7 and tee with 0. The pipeline reports 0; with pipefail it reports 7; PIPESTATUS holds 7 and 0.

  4. Submission. A harness runs systemd-run bench, which exits with 0 at once; bench exits 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 vv, its parent’s wait status ss satisfies WIFEXITED(s), and 𝚆𝙴𝚇𝙸𝚃𝚂𝚃𝙰𝚃𝚄𝚂(s)=vmod256∈{0,1,…,255},\mathtt{WEXITSTATUS}(s) = v \bmod 256 \in \{0, 1, \dots, 255\}, where vmod256v \bmod 256 is the remainder of vv divided by 256, taken in the range 0 to 255, so that −1mod256=255-1 \bmod 256 = 255. If the process was ended by signal number nn, WIFSIGNALED(s) holds, WTERMSIG(s) equals nn, 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:

  1. every process whose result matters ended normally with its expected status, which a test of a refusal sets to a nonzero value;

  2. the requested work was completed, not only submitted;

  3. the required outputs exist and pass the test oracle §10.5, the check that decides whether output is correct;

  4. 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 128+n128 + n 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).