Skip to Content
Mengty Learns
Foundations

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.

explanationintermediateTested with docker 29.4.3, kubernetes 1.37 (docs)Updated Nov 26, 2026

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 (signal 15) → exit 143 (128 + 15), unless your app catches SIGTERM and calls process.exit(0) (which exits 0).
  • SIGKILL (signal 9) → exit 137 (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)

Overview of Kubernetes Graceful Shutdown Sequence (terminationGracePeriodSeconds). 5 steps.
Overview5 steps in this flow

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.mjs

3. 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 exec still helps Ctrl+C work locally).
  • Cost / complexity trade-off: Using exec-form CMD and a 10-line SIGTERM handler is virtually free. Adding tini adds ~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". sh becomes PID 1, ignores SIGTERM, and every kubectl delete pod hangs for 30 seconds before dying with 137.
  • Confusing OOMKilled with slow shutdown: Both produce exit code 137 (SIGKILL). Always check kubectl describe pod (Reason: OOMKilled vs Completed/Error) rather than guessing from 137 alone.
  • Kubernetes removes Pod from EndpointSlice in parallel with SIGTERM: Traffic can still arrive for a few hundred milliseconds while kube-proxy updates rules; a short preStop sleep (sleep 2) prevents dropped requests during rolling updates.

Production checklist

  • Dockerfile uses JSON-array CMD/ENTRYPOINT (or exec "$@" in entrypoint scripts).
  • Application registers a SIGTERM handler that closes listeners and DB pools.
  • terminationGracePeriodSeconds is longer than your longest expected request or preStop hook.
  • Multi-process containers use tini or --init to 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