The Plain Picture#
The hotel manager can slip a note under a guest's door: "Please check out, you have 10 minutes." A polite guest packs up and leaves. If they ignore it, security removes them — no packing allowed.
The Real Names#
A signal is a small notification the kernel delivers to a process. The process can have a handler (a function that runs when the signal arrives) for most signals.
| Signal | Number | Meaning | Can the process handle it? |
|---|---|---|---|
SIGINT | 2 | "Interrupt" — what Ctrl+C sends | yes |
SIGTERM | 15 | "Please terminate" — the polite request | yes |
SIGKILL | 9 | "Die now" — no handler, no clean-up | no |
SIGHUP | 1 | terminal closed; many servers use it to reload config | yes |
Exit codes: when a process ends, it returns a number to its parent. 0 means success; anything
else means failure. If a signal killed it, shells report 128 + the signal number — so SIGKILL gives
137 (128 + 9), and SIGTERM gives 143 (128 + 15). Now you can see why OOMKilled from Ch 5 shows
exit code 137: the OOM killer uses SIGKILL.
Under the Hood — graceful shutdown#
When you deploy a new version of StudyBuddy's server, the old one must stop. Done well:
1. orchestrator sends SIGTERM ("please finish up")
2. app stops accepting new requests
3. app finishes the requests in progress, closes database connections
4. app exits with code 0
── if it's still alive after a grace period ──
5. orchestrator sends SIGKILL (immediate death, in-progress requests fail)Docker's docker stop waits 10 seconds by default before SIGKILL; Kubernetes waits 30 seconds by
default. You'll configure this in DevOps (coming soon).
The PID 1 trap: inside a container, your app is often PID 1 (Ch 3). Linux gives PID 1 special
treatment: signals with no handler installed are ignored rather than killing it. If your app doesn't
handle SIGTERM, docker stop hangs for the whole 10 seconds and then kills it hard. The fixes (a proper
signal handler, or a tiny init process like tini / docker run --init) come in Docker (coming soon).
Zombies and orphans (short, because interviewers occasionally ask):
A zombie is a process that has exited but whose parent hasn't collected its exit code yet. It uses no memory, just a slot in the process table.
An orphan is a process whose parent died first; it's adopted by PID 1.
Check yourself
3 questionsWhat's the difference between SIGTERM and SIGKILL?
Show answerHide answer
SIGTERM politely asks a process to exit and can be handled; SIGKILL kills immediately and can't be caught.
What exit code means success? What does 137 mean?
Show answerHide answer
0 is success; 137 = 128 + 9, killed by SIGKILL (often the OOM killer).
Describe a graceful shutdown in 4 steps.
Show answerHide answer
Receive SIGTERM → stop accepting new work → finish in-progress work and close connections → exit 0.