Linux Signals
Signal delivery, common signals, signal masking, SIGTERM vs SIGKILL, and graceful shutdown patterns.
Overview
Signals are asynchronous notifications sent to a process by the kernel, another process, or the process itself. There are 64 standard signals. Signals interrupt normal execution and invoke a registered handler, the default action (usually terminate or ignore), or are blocked via a signal mask.
Key Signals for Production
- SIGTERM (15) — request graceful termination; processes can catch and clean up
- SIGKILL (9) — unconditional termination; cannot be caught, blocked, or ignored
- SIGINT (2) — Ctrl+C from the terminal
- SIGHUP (1) — trigger config reload in daemons
- SIGCHLD (17) — sent to parent when a child changes state
- SIGUSR1 / SIGUSR2 (10/12) — user-defined signals
Signal Delivery
If a process has a signal blocked in its signal mask, it is queued (pending). Signals are not queued past one — if the same signal fires three times while blocked, only one delivery occurs when unblocked. Real-time signals (SIGRTMIN to SIGRTMAX) are queued and ordered.
Graceful Shutdown Pattern
Receive SIGTERM → stop accepting new work → drain in-flight requests → close connections → exit 0. Kubernetes uses terminationGracePeriodSeconds for this.
Key concepts
- SIGTERM (15)
- Request graceful shutdown. Can be caught and handled. Always try first — allows the process to flush buffers and exit cleanly.
- SIGKILL (9)
- Unconditional kill. Cannot be caught, blocked, or ignored. Use as a last resort after SIGTERM has been given time to work.
- SIGHUP (1)
- Modern convention: reload configuration without restarting. nginx, sshd, and most daemons handle this via a registered SIGHUP handler.
- Signal mask
- Per-thread bitmask of blocked signals. Blocked signals are queued (pending) and delivered when unblocked.
- Real-time signals
- SIGRTMIN through SIGRTMAX. Unlike standard signals, they are queued, ordered, and can carry a payload.
When to use it
- Explaining why kill -9 should be the last resort — it prevents graceful cleanup
- Designing container shutdown sequences in Kubernetes (preStop hooks + SIGTERM + grace period)
- Debugging why a daemon does not pick up config changes — likely needs SIGHUP
Interview questions
- What happens when you run kill -9 on a process vs kill -15? When should you use each?
- How do you ensure your Docker container shuts down gracefully when Kubernetes drains a node?
Sign up free to read the full topic
More Linux topics
Start free on Cariara · Coding problems · System design studio