Graceful Shutdowns, SIGTERM vs SIGKILL & Signal Handling
Handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL.
In plain words
Design shutdown latency around the slowest legitimate operation and verify the signal reaches the actual service process.
Think of it like…
A restaurant stops seating customers, serves the existing tables, closes the kitchen and only then turns off the lights.
Where you’ve seen it
In practice, STOPSIGNAL SIGTERM; docker stop --time 45 api is the concrete move for this job: handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. The release is not accepted until open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds confirms the result.
Words you’ll hear
- SIGTERM
- The implementation control in
STOPSIGNAL SIGTERM; docker stop --time 45 apifor handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. - SIGKILL
- The result to observe with
open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds, rather than infer from a successful command alone. - Grace Period
- The production boundary exposed when setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
Visual diagram
Theory & deep dive
SIGTERM control path
The concrete mechanism is STOPSIGNAL SIGTERM; docker stop --time 45 api. The observable result is open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds; if those disagree, inspect the runtime evidence before changing unrelated settings.
Handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. Keep SIGTERM, SIGKILL and Grace Period in the same deployment record so the intent and the observed state cannot drift apart.
STOPSIGNAL SIGTERM; docker stop --time 45 api
open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds, not with command success alone.Grace Period failure boundary
A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence. The narrow failure to design for is setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
Accept this compromise explicitly: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes. The controlled check-open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds-also identifies the point where a different design is safer.
Exercise this failure safely: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.
- Intent
- SIGTERM
When to use it & trade-offs
What it is
docker stop sends SIGTERM to PID 1 and waits the stop grace period before SIGKILL. The app should stop accepting work, drain or finish in-flight work, flush state and exit; orchestration readiness and connection timeouts must line up with the grace window.
Why it matters: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.
When to reach for it
- The fit is strongest for this job: handle SIGTERM for graceful shutdown before Docker escalates to SIGKILL. Pair it with
open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 secondsin the runbook. - Production SIGTERM changes should make SIGKILL observable through
STOPSIGNAL SIGTERM; docker stop --time 45 api, rather than trusting the runtime default. - Keep a rollback example for the case where setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
When to avoid or be careful
- Do not accept command success while the system can still fail by setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.
- Avoid erasing Grace Period protection merely to dodge this cost: “Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes”.
- Do not replace the evidence path-
open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds-with a screenshot or a mutable tag.
STOPSIGNAL SIGTERM; docker stop --time 45 api: practical choices
Strengths: docker stop sends SIGTERM to PID 1 and waits the stop grace period before SIGKILL. The app should stop accepting work, drain or finish in-flight work, flush state and exit; orchestration readiness and connection timeouts must line up with the grace window.
Watch out: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.
Strengths: It can begin with STOPSIGNAL SIGTERM; docker stop --time 45 api and fewer surrounding controls.
Watch out: It silently accepts setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically, which makes the result hard to reproduce after promotion.
Strengths: A shell-form ENTRYPOINT leaves sh as PID 1 and may not forward signals; a graceful handler that waits forever ends in SIGKILL. Exit code 143 commonly means SIGTERM, and 137 commonly means SIGKILL but needs OOM/context evidence.
Watch out: The platform adds an ownership and debugging layer; retain open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds as the direct check.
Interview signals & numbers to know
When this topic comes up
- STOPSIGNAL SIGTERM; docker stop --time 45 api
- open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds
- Failure signature: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically
- Trade-off: Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.
Benchmarks & rules of thumb
- 45 seconds
- example stop grace
- SIGTERM 15
- signal number 15 on typical Linux
- SIGKILL 9
- uncatchable escalation
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
STOPSIGNAL SIGTERM; docker stop --time 45 apias proof and skippingopen one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds. - Setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically
- Optimizing SIGTERM without accepting that graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes.
Likely interviewer follow-ups
- Why does
STOPSIGNAL SIGTERM; docker stop --time 45 apiaddress SIGTERM here rather than through application code? - How would you distinguish healthy SIGKILL from the failure “setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically” using
open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds? - When would this trade-off-“Graceful draining protects correctness but adds deploy latency; a hard stop limit remains necessary for stuck processes”-justify a different Grace Period design?
STOPSIGNAL SIGTERM; docker stop --time 45 api-and one proof-open one long request, call docker stop, observe no new accepts, request completion, flush log and exit code/timestamps before 45 seconds. Then name the boundary: setting stop timeout shorter than the app’s drain deadline, or expecting a /bin/sh wrapper to forward SIGTERM to the child automatically.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Graceful Shutdowns, SIGTERM vs SIGKILL & Signal Handling with generative AI interview Q&A and hands-on practice.