Linux Processes, Signals, and Exit Codes
How PID 1, SIGTERM, graceful shutdown, SIGKILL, exit codes 137 and 143, exec in entrypoints, and tini/--init keep container shutdowns clean.
TL;DR
Linux sends SIGTERM (15) to PID 1 first, giving your app a grace window (30s default in Kubernetes) to drain requests before sending uncatchable SIGKILL (9, exit 137). Use JSON-array CMD/ENTRYPOINT or exec so a shell doesn't swallow SIGTERM, and add tini (--init) if your app spawns child processes.
Why it exists
During a rolling deploy or scale-down, dropping active HTTP requests causes 502 Bad Gateway errors for users. Graceful shutdown only works if the Linux kernel's SIGTERM actually reaches your application process and your code handles it before SIGKILL arrives.
Mental model
In Linux, exit codes above 128 encode 128 + signal_number:
SIGTERM(signal15) → exit143(128 + 15), unless your app catchesSIGTERMand callsprocess.exit(0)(which exits0).SIGKILL(signal9) → exit137(128 + 9). Cannot be caught or ignored; either the shutdown grace period expired or the kernel OOM killer terminated the process.
PID 1 is special in Linux: the kernel ignores default signal dispositions for PID 1 unless the process explicitly installs a signal handler, and PID 1 is responsible for reaping orphaned child ("zombie") processes.
▶ Flow: watch it work
Kubernetes Graceful Shutdown Sequence (terminationGracePeriodSeconds)
Press Start or tap any step dot to trace the path.
▸All steps (5)
Do it
1. Avoid the shell-form CMD trap
Use the exec form (["node", "server.mjs"]) in your Dockerfile, or exec "$@" in an entrypoint script, so the shell does not sit at PID 1 and swallow SIGTERM:
# Good: node runs directly as PID 1
CMD ["node", "server.mjs"]#!/bin/sh
# Good: exec replaces the shell with the app binary (not run)
set -eu
exec "$@"2. Use tini / --init when spawning subprocesses
Node.js or Python apps that fork worker processes do not reap zombie children if they run as PID 1. Wrap them with tini (or docker run --init):
# Run with Docker's built-in tini init at PID 1 (not run)
docker run --init --rm node:22-alpine3.21 node server.mjs3. Handle SIGTERM in application code
// Minimal graceful shutdown handler in Node.js (not run)
process.on('SIGTERM', () => {
server.close(() => {
process.exit(0);
});
});When you DON'T need this
- Static sites or serverless functions: Static exports on a CDN or managed isolates (like Cloudflare Workers) handle request draining and process lifecycle in the platform—there is no PID 1 for you to manage.
- Short-lived CLI / batch containers: A one-off build or lint container that exits on completion doesn't need HTTP connection draining (though
execstill helpsCtrl+Cwork locally). - Cost / complexity trade-off: Using exec-form
CMDand a 10-lineSIGTERMhandler is virtually free. Addingtiniadds ~100 KB to an image and is only necessary if your container spawns child processes or your runtime doesn't handle PID 1 signal semantics.
Gotchas
- Shell-form
CMD node server.mjs: Docker wraps it in/bin/sh -c "node server.mjs".shbecomes PID 1, ignoresSIGTERM, and everykubectl delete podhangs for 30 seconds before dying with137. - Confusing OOMKilled with slow shutdown: Both produce exit code
137(SIGKILL). Always checkkubectl describe pod(Reason: OOMKilledvsCompleted/Error) rather than guessing from137alone. - Kubernetes removes Pod from EndpointSlice in parallel with
SIGTERM: Traffic can still arrive for a few hundred milliseconds whilekube-proxyupdates rules; a shortpreStopsleep (sleep 2) prevents dropped requests during rolling updates.
Production checklist
-
Dockerfileuses JSON-arrayCMD/ENTRYPOINT(orexec "$@"in entrypoint scripts). - Application registers a
SIGTERMhandler that closes listeners and DB pools. -
terminationGracePeriodSecondsis longer than your longest expected request orpreStophook. - Multi-process containers use
tinior--initto reap zombie processes.
Check yourself
Check Yourself
1. Every deploy of your Pod takes 30 seconds per replica to terminate and exits with code 137. What is the most likely cause?
2. Why does exec "$@" at the end of a Docker entrypoint script fix signal handling?
Sources
Networking Basics: DNS, TCP, TLS, HTTP
How a browser resolves a hostname, opens a TCP connection, negotiates TLS 1.3, and exchanges HTTP requests and responses.
Reverse Proxy & TLS Termination
How a reverse proxy terminates TLS with ACME/Let's Encrypt, routes traffic, sets forwarding headers, and compares Nginx, Caddy, and managed edges.