Skip to content

Lesson 8 of 20 · 18m

Signals and the Process Lifecycle: kill Is a Request

kill(2) is a request, SIGKILL is a kernel teardown, and PID 1 reaps zombies — the lifecycle rules Docker's grace period is built on.

kill(2) sends a request, not an order

The command is named kill, so everyone assumes it kills. What it actually does is deliver a signal — a small integer the kernel drops into the target task's pending-signal set. What happens next depends on what the process registered for that signal. SIGTERM (15) defaults to termination but is fully catchable; a process can install a handler that flushes buffers, drops locks, and exits cleanly — or a handler that ignores it entirely. SIGKILL (9) is the exception: it can't be caught, blocked, or ignored, because userspace never sees it — the kernel just schedules the task for teardown.

That distinction explains the entire lifecycle drama of production services. docker stop sends SIGTERM, waits the grace period (10 s default; Kubernetes terminationGracePeriodSeconds defaults to 30), then sends SIGKILL. An app with no SIGTERM handler doesn't shut down politely — it wastes the whole window and then dies mid-write anyway. A five-line handler and an honest trap in bash turns "mysterious 30-second deploy stall plus truncated state file" into "process exited in 200 ms with clean state."

Signals you'll actually meet

  • SIGINT (2): Ctrl-C. Catchable; shells forward it to the foreground process group.
  • SIGHUP (1): originally "your modem hung up" — the convention became "reload config" for daemons. systemctl reload nginx and kill -HUP both mean this.
  • SIGCHLD (17): parent notification that a child changed state — the hook wait() hangs on.
  • SIGSTOP/SIGCONT (19/18): uncatchable pause/resume; SIGTSTP is the catchable version Ctrl-Z sends.
  • SIGPIPE (13): writing to a closed pipe delivers this — why a | head pipeline makes the left side die quietly.
  • SIGSEGV (11), SIGBUS (7): memory violations; the ones that leave core dumps.

kill -l prints the full table. kill is both a bash builtin and /bin/kill — same syscall either way.

Zombies: the exit nobody collected

When a process exits, it leaves a task skeleton holding its exit code until a parent calls wait() to collect it. That's a zombie (Z state) — already dead, consuming no CPU, just an unreaped row in the task table. Orphans get reparented to PID 1, whose job includes reaping them.

This is where containers invented a fresh bug. A container's PID namespace makes your app PID 1 — and PID 1 has special duties. Most app runtimes never call wait() on adopted orphans, so every grandchild that dies stays a zombie forever. Run enough cron jobs or exec calls and pids.max arrives with a container full of [sh] <defunct>. Docker's --init flag drops in tini, a 20-kilobyte init whose only job is forwarding signals and reaping zombies; Kubernetes does it via shareProcessNamespace or a container runtime with an init. If your "container keeps getting slower" report shows hundreds of zombies, the fix isn't more memory — it's a real PID 1.

D-state: where even SIGKILL waits

SIGKILL ends a task the next time it's schedulable. If the task is in D — uninterruptible sleep inside a syscall, classically a dead NFS mount or a wedged block device — it can't run, so the kill pends until the syscall returns. Which may be never. This is why "even kill -9 doesn't work" is a real support ticket, and why the fix is umount -f/umount -l or a reboot, not a bigger signal.

Process groups and job control

Signals aim at a process group when you call kill(-pgid, sig) — setsid creates a new session and group, which is how daemons detach from your login's fate. pkill -g, kill -- -12345, and the shell's job control (Ctrl-Z → SIGTSTP, bg → SIGCONT) all operate on groups, which is why killing a pipeline doesn't always kill its members.

Try this

sleep 300 & kill -TSTP $!; ps -o pid,stat,cmd -p $!; kill -CONT $!
(sleep 1; exit 7) & wait $!; echo $?          # reaping in your own shell
docker run --rm --init alpine sh -c 'sleep 5' # tini as PID 1

Field notes

The kill -9 reflex deserves its reputation: Postgres distinguishes smart (wait for clients), fast (rollback, checkpoint, exit), and immediate (kill-style, needs recovery) shutdowns precisely because hard-killing a database mid-checkpoint costs you a crash-recovery pass — or a corrupted page on an unlucky day. Send SIGTERM, give it the documented grace window, escalate only when it ignores you.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.