Signals: how a process is asked to stop
You press Ctrl-C, run kill, or deploy a new version and the old container stops. Each time, the process receives a signal. Here is what a signal is, what a process can do with one, and how to shut down without losing the work in progress.
01 A signal is a number the kernel delivers
A signal is a small notification sent to a process. It carries almost nothing: just a number, such as 15. It can come from another process through the kill() system call, from the terminal when you press a key, or from the kernel itself, for example when the program touches memory it does not own.
| Signal | No. | Usually sent by | Default action |
|---|---|---|---|
| SIGINT | 2 | Ctrl-C in the terminal | terminate |
| SIGTERM | 15 | kill, docker stop, Kubernetes, systemd | terminate |
| SIGKILL | 9 | kill -9, the OOM killer, the end of a grace period | terminate; cannot be caught |
| SIGHUP | 1 | the terminal closing; by convention, “reload your config” | terminate |
| SIGQUIT | 3 | Ctrl-\ in the terminal | terminate and dump core |
| SIGSTOP / SIGTSTP | 19 / 20 | a debugger / Ctrl-Z | pause; SIGSTOP cannot be caught |
| SIGCHLD | 17 | the kernel, when a child process exits | ignore |
| SIGSEGV | 11 | the kernel, on a bad memory access | terminate and dump core |
| SIGPIPE | 13 | the kernel, on writing to a pipe nobody reads | terminate |
The kernel never interrupts a process in the middle of an instruction to hand it a signal. It sets a pending bit for that signal and acts on it when the process next returns from the kernel to its own code, which happens constantly: after every system call and every timer tick. Ordinary signals do not queue: two SIGTERMs that arrive before the first is handled count as one.
02 Three ways a process can respond
For each signal, a process has an action recorded in the kernel. It starts as the default, and the program can change it with sigaction() (or signal.signal() in Python, process.on() in Node).
SIGKILL and SIGSTOP cannot be handled, blocked or ignored. That is the point of them: they always work. It also means that after SIGKILL nothing of yours runs: no handler, no finally, no flushing of buffers, no deleting of lock files.
03 A graceful stop, step by step
A worker that takes jobs from a queue should finish the job it is on, take no new one, and exit. The handler does not do the shutdown itself. It only sets a flag, and the main loop checks the flag between jobs.
- just sent
- just changed
- finished cleanly
worker.py is PID 1 in a container, halfway through job 2. At start-up it registered on_stop() as its SIGTERM handler, so the kernel has that recorded for the process.
Try it. This is a real run on macOS; Linux prints the same.
Swap kill $! for kill -9 $! and the handler never runs. On Linux, bash reports it like this (a real run in a container; the PID will differ):
Why only a flag? A handler can run between any two steps of your program, perhaps while it holds a lock or is half-way through updating a data structure. In C, only a short list of async-signal-safe functions (such as write()) may be called from a handler; printf() and malloc() are not on it. CPython is gentler: its C handler only notes the signal, and your Python handler runs later in the main thread between bytecodes. Keeping handlers to “set a flag” is safe everywhere.
04 SIGTERM first, SIGKILL after a grace period
Everything that stops processes for you follows the same two-step pattern: ask politely with SIGTERM, wait a while, then force it with SIGKILL. The wait is your budget for a clean shutdown.
| Tool | Sends first | Waits | Setting |
|---|---|---|---|
| docker stop | SIGTERM (or the image’s STOPSIGNAL) | 10 s | docker stop -t |
| Kubernetes | SIGTERM, after any preStop hook | 30 s | terminationGracePeriodSeconds |
| systemd | SIGTERM (KillSignal=) | 90 s | TimeoutStopSec |
The trap is a program with no handler running as PID 1, which is what your app usually is inside a container. The kernel shields each PID namespace’s first process from signals it has not set a handler for, so the default “terminate” never happens.
- just sent
- dropped or cut off
- still running
This time the program installed no SIGTERM handler, so its action is the default. It is PID 1 in its container, which matters in a moment.
A real run with a handler-less Python loop as the container’s main process. The stop takes the full grace period, and the exit code says it was killed:
Two fixes: install a SIGTERM handler, or run a tiny init in front of your app (docker run --init, or tini in the image) that is PID 1 instead, passes signals on and reaps exited children. With worker.py from above, the same docker stop returns as soon as the current job ends.
05 Send and inspect signals yourself
Here is /proc/1/status for worker.py running as PID 1 in a container, from a real run. SigCgt is the set of caught signals: 0x4002 has bits 1 and 14 set, so signals 2 (SIGINT) and 15 (SIGTERM) have handlers. SigIgn shows signals 13 (SIGPIPE) and 25 (SIGXFSZ), which CPython ignores at start-up.
Keys in the terminal send signals to the whole foreground process group, not one process: Ctrl-C sends SIGINT to every process in a pipeline such as cat log | grep x.
06 When it bites
CMD python app.pyA shell in the middleThe shell form of CMD runs your app through /bin/sh -c. Depending on the shell, sh may stay as PID 1 and not pass SIGTERM on. Use the exec form, CMD ["python", "app.py"], or exec in an entry script.SIGTERM → exit at onceStopping before traffic moves awayIn Kubernetes, removing a pod from the Service and sending SIGTERM happen at about the same time. A server that quits instantly drops requests still being routed to it. Keep serving for a few seconds, for example with a preStop sleep, then drain.grace 10 s, drain 30 sA shutdown longer than the grace periodIf finishing in-flight work takes longer than the grace period, SIGKILL arrives anyway. Raise the period or cut the work into shorter pieces.kill -9 firstReaching for SIGKILL firstIt skips every cleanup: temporary files stay, lock files stay, buffered log lines are lost, a database client never says goodbye. Send SIGTERM and wait first.print | head -1SIGPIPE ends a pipelineWhen head exits, the writer gets SIGPIPE on its next write and, by default, dies quietly. Usually that is what you want; in a server writing to a closed socket it is not, so servers ignore SIGPIPE and handle the write error.