Stopping a process is a conversation, not a command. Here's how it goes, and where it breaks.
Signals you'll actually use
| Signal | Number | Default action | Typical use |
|---|---|---|---|
| SIGHUP | 1 | terminate | many daemons reload config (nginx -s reload sends it) |
| SIGINT | 2 | terminate | Ctrl+C |
| SIGKILL | 9 | terminate, can't be caught | last resort |
| SIGTERM | 15 | terminate | polite stop: kill, docker stop, kubelet |
| SIGSTOP / SIGCONT | 19 / 18 | stop / resume | pause a process (SIGSTOP can't be caught either) |
The numbers above are for x86 and ARM Linux. If a process is killed by signal N, a shell or container runtime reports exit code 128 + N: 143 is SIGTERM and 137 is SIGKILL.
kill <pid> # SIGTERM kill -HUP <pid> # reload kill -9 <pid> # SIGKILL, no cleanup kill -0 <pid> # signal nothing, just check the process exists pkill -TERM -f 'gunicorn: master'
How graceful shutdown works
docker stop sends SIGTERM to PID 1 and, after 10 seconds, SIGKILL. Kubernetes runs any preStop hook, sends SIGTERM, and sends SIGKILL after terminationGracePeriodSeconds (default 30). The countdown includes the preStop hook. In the SIGTERM handler, stop accepting new work, finish in-flight requests, close pools, and exit.
PID 1 is special
Inside a container, your entrypoint is PID 1 in its own PID namespace. Two rules change:
- No default actions. The kernel ignores SIGTERM and SIGINT sent to PID 1 unless it has installed a handler. An app without one ignores
docker stopuntil SIGKILL arrives. - It must reap orphans. Any process whose parent exits is re-parented to PID 1. If PID 1 never calls
wait(), those processes become zombies once they exit.
Shell-form commands make this worse:
CMD node server.js # shell form: may run under /bin/sh -c CMD ["node", "server.js"] # exec form: node is PID 1 and gets SIGTERM (it still needs a handler)
For a wrapper script, end it with exec "$@" or exec node server.js so the app replaces the shell. To get a real init process, use docker run --init, tini as the ENTRYPOINT, or in Kubernetes shareProcessNamespace: true.
Zombies and orphans
- Zombie (
Z,<defunct>): the process has exited, but its parent hasn't collected the exit status. It can't be killed, because it's already dead. - Orphan: a live process whose parent died. It's re-parented to PID 1 or a subreaper.
ps -eo pid,ppid,stat,cmd | awk '$3 ~ /Z/' # zombies and their parent PID ps -o pid,ppid,cmd -p <ppid> # the parent that isn't reaping
Common wrong fix
Making kill -9 the default way to stop things, including docker kill in deploy scripts and --grace-period=0 --force in Kubernetes. Every stop becomes a crash: in-flight requests are cut off, connections to the DB aren't closed, and queue messages are processed twice. If SIGTERM doesn't stop the app, find the reason. Usually it's PID 1 without a handler, or a shell that doesn't forward the signal. Fix that instead of skipping straight to SIGKILL.