Signals (SIGKILL, SIGTERM)
How one process, or the kernel, taps a running program on the shoulder: what a signal is, what a handler does, and why SIGTERM asks while SIGKILL does not.
In 30 seconds
A signal is a small numbered notification that the kernel delivers to a running program. Pressing Ctrl+C sends one; so does the kill command.
It can arrive at any moment. For most signals the program may install a handler, a function that runs when the signal arrives, or choose to ignore it.
SIGTERM (15) is a request to stop that a program can catch and use to shut down cleanly. SIGKILL (9) cannot be caught: the kernel ends the program on the spot.
Signals (SIGKILL, SIGTERM), step by step
Numbers are those of Linux on x86 and ARM. 1, 2, 9 and 15 are the same on other Unix systems; SIGSTOP is 17 on macOS and the BSDs. Windows has no direct equivalent of most signals.
- Step 1: A number sent to a process. A signal is a short notification sent to a running program. One process asks with the kill() call, the kernel checks it is allowed, and then delivers the signal. The command kill 4242 sends number 15, SIGTERM, to the process with ID 4242.
- Step 2: The common signals. Each signal has a name, a number and a default action. Some come from people (Ctrl+C sends SIGINT), some from other programs (kill sends SIGTERM), and some from the kernel itself: SIGSEGV is what a process receives when it touches memory it does not own.
- Step 3: It arrives at any moment. Signals are asynchronous: the process did not ask for one and cannot know when it will come. The kernel records the signal as pending, then delivers it the next time the process is about to continue its own code, which can be between any two instructions.
- Step 4: Three possible outcomes. For each signal a process has a setting, called its disposition. Left alone, the kernel applies the default action, which for most signals ends the process. The process can instead ask to ignore the signal, or register a handler: a function of its own to call.
- Step 5: A handler interrupts the code. With a handler registered, delivery works like an unplanned function call. The normal code is paused where it stands, the handler runs, and the code resumes at the same spot. Because it can cut in anywhere, a handler should do very little: here it only sets a flag for the main loop to check.
- Step 6: Two that cannot be caught. SIGTERM reaches the process, so a handler can react to it. SIGKILL never does: the kernel simply ends the process, and no handler, ignore setting or block applies. The same holds for SIGSTOP, which pauses. They exist so that an administrator can always stop or pause a misbehaving program.
- Step 7: Graceful shutdown. A well-behaved server catches SIGTERM, finishes the requests in hand, saves its data, closes its files and exits by itself. Tools such as docker stop rely on this: they send SIGTERM, wait a grace period (10 seconds by default), and send SIGKILL only if the process is still there.
Try it yourself
Learn this first
Spotted a mistake on this page?