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 returnsnfrommain), or the kernel ends it because of a signal, such as SIGKILL or SIGSEGV. exit(n)keeps only the lowest 8 bits ofn, which hold 0 to 255.exit 256gave 0,exit 300gave 44 (300 − 256), andexit -1gave 255.- An ended process stays in the kernel’s process table, as a zombie, until its parent calls
wait()orwaitpid(). 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 3it was0x300: the code sits in the second byte. For a kill by SIGKILL it was0x9: the signal number sits in the lowest bits. The macrosWIFEXITEDandWEXITSTATUS, orWIFSIGNALEDandWTERMSIG, 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):
| Signal | Sent by | Handler allowed | Exit code |
|---|---|---|---|
| SIGTERM (15) | kill PID, docker stop, Kubernetes stopping a container | Yes | 143 |
| SIGKILL (9) | kill -9 PID, the kernel when memory runs out | No | 137 |
| SIGINT (2) | Ctrl+C in a terminal | Yes | 130 |
| SIGSEGV (11) | The kernel, on a bad memory access | Yes | 139 |
| SIGABRT (6) | abort(), which ends the process even if a handler runs | Yes | 134 |
| SIGPIPE (13) | The kernel, on a write to a pipe with no reader | Yes | 141 |
killsends any signal by name or number, and SIGTERM by default:kill -15 PID,kill -TERM PID, andkill PIDsend 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,
subprocessgavereturncode-9 for SIGKILL. In Go,ExitError.ExitCode()gave -1, the error read “signal: killed”, andsyscall.WaitStatusheld the signal. - A code above 128 is ambiguous: a program may call
exit(137)itself. Kubernetes’sreasonfield and Docker’sOOMKilledflag 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 stopsends SIGTERM, waits 10 seconds, then sends SIGKILL.sleep 1000as PID 1 has no SIGTERM handler, so it ignored SIGTERM.docker stoptook 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 tosleep.sleepisn’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
| Code | Meaning | Common cause |
|---|---|---|
| 0 | Success | The process finished its work. |
| 1 | General failure | An unhandled exception in Python, a failed cd in bash, a program’s own error. |
| 2 | Misuse | A bash builtin with a bad option or a missing argument; ls with a bad flag. |
| 125 | Docker failed | docker run itself failed, such as on an unknown flag. |
| 126 | Can’t execute | The file exists but isn’t executable. |
| 127 | Not found | No such command in PATH. |
| 128 | Start error | Kubernetes with containerd: the container’s command couldn’t start. |
| 130 | 128 + SIGINT (2) | Ctrl+C. |
| 134 | 128 + SIGABRT (6) | A call to abort(), such as from a failed C assert(). |
| 137 | 128 + SIGKILL (9) | Out of memory, a stop that outlasted its grace period, docker kill. |
| 139 | 128 + SIGSEGV (11) | Segmentation fault: a bad memory access in native code. |
| 141 | 128 + SIGPIPE (13) | Writing to a pipe whose reader has closed. |
| 143 | 128 + SIGTERM (15) | Stopped by SIGTERM, with no handler to exit cleanly. |
| 255 | Out of range | exit(-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
Terminatedstate, with anexitCode, areason, and start and finish times. - The
reasonisCompletedfor exit code 0,Errorfor another code,OOMKilledwhen the kernel ended it for using too much memory, andStartErrorwhen its command never started. - A pod’s
restartPolicydecides whether a stopped container starts again:Always,OnFailure(only after a non-zero code), orNever. After a restart, the previous run’s status moves tolastState. - Restarts wait 10 s, 20 s, 40 s, and so on, up to 5 minutes, and the pod shows
CrashLoopBackOffwhile 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. ApodFailurePolicyrule withonExitCodesacts on given codes:FailJobends the Job at once,Ignoredoesn’t count the failure, andCountcounts 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.
| Scenario | Kubernetes | Docker |
|---|---|---|
exit 0 | 0, Completed | 0 |
exit 1 | 1, Error | 1 |
Command not in PATH | 128, StartError | 127 |
| File not executable | 128, StartError | 126 |
| Segmentation fault in Python | 139, Error | 139 |
| Main process over a 64 MiB memory limit | 137, OOMKilled | 137, OOMKilled: true |
| Child process over the limit | 137, OOMKilled | Container kept running |
Liveness probe fails, sleep is PID 1 | 137 after 6 s | — |
Liveness probe fails, sleep isn’t PID 1 | 143 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
OOMKilledreason, or Docker’sOOMKilled: 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.groupat 0, so the kernel killed only the Python child.shsaw 137 from its child and kept running, so the container did too. Docker still setOOMKilled: true. - On cgroup v2 the kubelet sets
memory.oom.groupto 1, so the kernel killed every process in the container, and the container ended. The kubelet settingsingleProcessOOMKill: trueturns 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
sleepas 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.sleepwasn’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
StartErrorand an error message from runc, the low-level tool that starts the container’s process, such asexec: "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, andCrashLoopBackOfffollows 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