Exit codes

Created · Updated

An exit code is the number from 0 to 255 that a process leaves behind when it ends: 0 means success, and any other value means failure. A container is a process, so its exit code is that process’s exit code, and Docker and Kubernetes report it so a person or a controller can tell how it ended. The alternative would be reading logs for every stopped container. One small number answers the first question: did the process finish its work, fail on its own, or get killed?

An exit code comes from three places in turn:

  • The process ends, either by calling exit(n) or because the kernel ends it with a signal.
  • Its parent process collects how it ended with wait().
  • Shells, Docker, and Kubernetes turn that into one number. A process killed by signal n gets 128+n.

How a process ends

  • A process ends in one of two ways. It calls exit(n) (or returns n from main), or the kernel ends it because of a signal, such as SIGKILL or SIGSEGV.
  • exit(n) keeps only the lowest 8 bits of n, which hold 0 to 255. exit 256 gave 0, exit 300 gave 44 (300 − 256), and exit -1 gave 255.
  • An ended process stays in the kernel’s process table, as a zombie, until its parent calls wait() or waitpid(). The call returns a wait status and removes the zombie.
  • The wait status is one integer that holds either the exit code or the signal, never both. For exit 3 it was 0x300: the code sits in the second byte. For a kill by SIGKILL it was 0x9: the signal number sits in the lowest bits. The macros WIFEXITED and WEXITSTATUS, or WIFSIGNALED and WTERMSIG, read the two cases.

Signals

A signal is a small numbered message the kernel delivers to a process, such as “terminate” or “you accessed bad memory”. Another process sends one with the kill() system call, and the kernel sends some itself. Each signal has a default action, usually ending the process. A process can install a handler, a function that runs instead of the default action, for most signals.

These signals cause the common codes. Their numbers are those of x86 and ARM, from signal(7):

SignalSent byHandler allowedExit code
SIGTERM (15)kill PID, docker stop, Kubernetes stopping a containerYes143
SIGKILL (9)kill -9 PID, the kernel when memory runs outNo137
SIGINT (2)Ctrl+C in a terminalYes130
SIGSEGV (11)The kernel, on a bad memory accessYes139
SIGABRT (6)abort(), which ends the process even if a handler runsYes134
SIGPIPE (13)The kernel, on a write to a pipe with no readerYes141
  • kill sends any signal by name or number, and SIGTERM by default: kill -15 PID, kill -TERM PID, and kill PID send the same signal.
  • A handler can clean up and then exit with any code it likes. Without a handler, all six signals above end the process. SIGSEGV and SIGABRT may also write a core dump, a file with the process’s memory for debugging.
  • SIGKILL, like SIGSTOP, can’t be handled, blocked, or ignored. So SIGTERM is a request a process can act on, and SIGKILL is not.
  • A few signals have other numbers on some architectures, but SIGKILL is 9 and SIGTERM is 15 everywhere.

128+n

  • A process killed by a signal has no exit code; its wait status holds only the signal. 128+n is a convention for turning it into one number: bash sets $? to 128+n when a command dies from signal n. Docker, containerd, and Kubernetes report the same numbers.
  • Programming languages show a signal death their own way. In Python, subprocess gave returncode -9 for SIGKILL. In Go, ExitError.ExitCode() gave -1, the error read “signal: killed”, and syscall.WaitStatus held the signal.
  • A code above 128 is ambiguous: a program may call exit(137) itself. Kubernetes’s reason field and Docker’s OOMKilled flag tell some of the cases apart.

PID 1 in a container

  • A container’s main process runs in its own PID namespace, where it has process ID 1. The kernel treats PID 1 specially: it delivers a signal to it only if the process installed a handler for that signal.
  • The container runtime, outside the namespace, can still force SIGKILL. Inside the container, nothing can: bash as PID 1 survived both SIGTERM and SIGKILL sent by another process in the container, even as root.
  • docker stop sends SIGTERM, waits 10 seconds, then sends SIGKILL. sleep 1000 as PID 1 has no SIGTERM handler, so it ignored SIGTERM. docker stop took 10.2 s, and the container exited with 137.
  • With docker run --init, a small init process, tini, is PID 1 and passes SIGTERM on to sleep. sleep isn’t PID 1, so SIGTERM ended it at once, and the container exited with 143.
  • A Dockerfile’s shell form can put a shell as PID 1, between the stop signal and the program.

Common codes

CodeMeaningCommon cause
0SuccessThe process finished its work.
1General failureAn unhandled exception in Python, a failed cd in bash, a program’s own error.
2MisuseA bash builtin with a bad option or a missing argument; ls with a bad flag.
125Docker faileddocker run itself failed, such as on an unknown flag.
126Can’t executeThe file exists but isn’t executable.
127Not foundNo such command in PATH.
128Start errorKubernetes with containerd: the container’s command couldn’t start.
130128 + SIGINT (2)Ctrl+C.
134128 + SIGABRT (6)A call to abort(), such as from a failed C assert().
137128 + SIGKILL (9)Out of memory, a stop that outlasted its grace period, docker kill.
139128 + SIGSEGV (11)Segmentation fault: a bad memory access in native code.
141128 + SIGPIPE (13)Writing to a pipe whose reader has closed.
143128 + SIGTERM (15)Stopped by SIGTERM, with no handler to exit cleanly.
255Out of rangeexit(-1), whose lowest 8 bits are 255.

Codes 64 to 78 are named in BSD’s sysexits.h, such as 64 for a usage error. Few programs use them.

In Kubernetes

  • The kubeletkubelet The agent that runs on each node in a Kubernetes cluster and makes sure the containers in its pods are running. asks the container runtimecontainer runtime The software on a node that runs containers. It pulls images and asks the kernel to set up each container. for each container’s status and writes it into the podpod The smallest unit Kubernetes runs: one or more containers that run on the same node and share some resources, such as a network namespace.’s status. A stopped container is in the Terminated state, with an exitCode, a reason, and start and finish times.
  • The reason is Completed for exit code 0, Error for another code, OOMKilled when the kernel ended it for using too much memory, and StartError when its command never started.
  • A pod’s restartPolicy decides whether a stopped container starts again: Always, OnFailure (only after a non-zero code), or Never. After a restart, the previous run’s status moves to lastState.
  • Restarts wait 10 s, 20 s, 40 s, and so on, up to 5 minutes, and the pod shows CrashLoopBackOff while it waits. A container that runs for 10 minutes resets the delay.
  • To stop a container, the kubelet sends SIGTERM, or the image’s own stop signal, to its main process. It waits for the grace period, terminationGracePeriodSeconds, 30 seconds by default, then sends SIGKILL.
  • A Job, which runs pods until they succeed, counts failed pods against backoffLimit, 6 by default. A podFailurePolicy rule with onExitCodes acts on given codes: FailJob ends the Job at once, Ignore doesn’t count the failure, and Count counts it as usual.

Common scenarios

Each scenario ran as a pod on a kind cluster (Kubernetes 1.37, containerd 2.3, cgroup v2) and as a container on Docker 29, where it applies. The pods had a 5-second grace period.

ScenarioKubernetesDocker
exit 00, Completed0
exit 11, Error1
Command not in PATH128, StartError127
File not executable128, StartError126
Segmentation fault in Python139, Error139
Main process over a 64 MiB memory limit137, OOMKilled137, OOMKilled: true
Child process over the limit137, OOMKilledContainer kept running
Liveness probe fails, sleep is PID 1137 after 6 s—
Liveness probe fails, sleep isn’t PID 1143 after 2 s—
docker stop, sleep is PID 1—137 after 10.2 s
docker stop with --init—143 after 0.1 s

Out of memory:

  • A memory limit is enforced by the container’s cgroup. When the processes in it reach the limit and the kernel can’t free memory, the kernel’s out-of-memory (OOM) killer sends SIGKILL, so the code is 137.
  • Only the OOMKilled reason, or Docker’s OOMKilled: true, tells this apart from any other SIGKILL.

Out of memory in a child process, with sh as the main process and Python as its child:

  • Docker left the cgroup setting memory.oom.group at 0, so the kernel killed only the Python child. sh saw 137 from its child and kept running, so the container did too. Docker still set OOMKilled: true.
  • On cgroup v2 the kubelet sets memory.oom.group to 1, so the kernel killed every process in the container, and the container ended. The kubelet setting singleProcessOOMKill: true turns this off.

Stopped by Kubernetes:

  • A liveness probe is a check the kubelet runs on a container; when it fails, the kubelet stops the container and starts it again. The test probe always failed.
  • With sleep as PID 1, SIGTERM was ignored, and SIGKILL came when the 5-second grace period ended: 137, 6 seconds after the container started.
  • With shareProcessNamespace: true, all containers in the pod share one PID namespace, and PID 1 is the pod’s pause container, a tiny process Kubernetes starts in every pod. sleep wasn’t PID 1, so SIGTERM ended it at once: 143, 2 seconds after start.
  • Deleting a pod stops its containers the same way. So does eviction, when the kubelet removes pods from a node that runs short of memory, disk, or process IDs, except that a hard eviction gives no grace period at all.
  • A program that handles SIGTERM and calls exit(0) ends with 0.

Other scenarios:

  • Command not found: Kubernetes with containerd reports 128 with reason StartError and an error message from runc, the low-level tool that starts the container’s process, such as exec: "nosuchcmd": executable file not found in $PATH. Docker reports 127 or 126, the codes a shell would give.
  • Crash in native code: a segmentation fault gives 139, and abort() gives 134. The kernel sends the signal, so the process writes no error message of its own.
  • Exit code 0 with restartPolicy: Always: a Deployment’s pods use this policy, so a container that finishes its work is started again anyway, and CrashLoopBackOff follows even though every run succeeded.

References: exit(3), wait(2), signal(7), kill(1), abort(3), pid_namespaces(7), Bash exit status, docker run exit status, Pod lifecycle, Jobs, Node-pressure eviction, Kubelet configuration, Control Group v2