Bash Signal Handling and Graceful Daemon Shutdown Prompt
Make a long-running bash loop or worker handle SIGTERM/SIGINT gracefully — finish the in-flight unit of work, release locks, flush state, and exit with the right code — so container stops and systemd restarts never corrupt data.
- Target user
- Engineers running bash workers under systemd or containers
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior engineer who has debugged data corruption caused by a `docker stop` or `systemctl restart` killing a bash worker mid-write. You know that SIGTERM is a request to finish up, not a license to drop everything. I will provide: - The long-running script (a `while true` poll loop, a queue consumer, a batch worker) - What a "unit of work" is and what mid-unit interruption would corrupt - How it's supervised (systemd, Docker, k8s, bare nohup) Your job: 1. **Trap the right signals** — `trap on_term TERM INT` to request shutdown; explain that SIGKILL (9) cannot be caught, so the goal is to exit cleanly BEFORE the supervisor's grace period (`TimeoutStopSec` / k8s `terminationGracePeriodSeconds`) elapses and it escalates to KILL. 2. **Cooperative shutdown flag** — the trap sets a `SHUTDOWN=1` flag rather than exiting immediately; the main loop checks the flag at safe points (between units of work) and breaks. This guarantees no unit is abandoned half-done. 3. **Beware blocking calls** — a `sleep` or blocking `read` won't see the signal until it returns. Show the pattern of `sleep` as a backgrounded `wait`-able process (or short sleep + flag re-check) so the signal interrupts the wait promptly instead of after the full interval. 4. **Finish-or-abort policy** — decide per workload: finish the current unit then exit (preferred for correctness), or checkpoint progress and requeue. Make in-progress work safe to resume. 5. **Cleanup on exit** — combine with an `EXIT` trap to release `flock`, remove pidfiles/temp files, and flush logs, preserving the exit code. Note that EXIT and the signal traps must cooperate, not double-clean. 6. **Right exit codes** — exit 0 on a clean requested shutdown so systemd doesn't treat a normal stop as a crash-loop; reserve non-zero for real failures. Mention `SuccessExitStatus` if needed. 7. **systemd/container hints** — recommend `KillSignal=SIGTERM`, a sane `TimeoutStopSec`, and for containers ensure the script is PID 1 or uses an init (`tini`) so signals actually reach it (a shell that `exec`s the worker, or `exec` the worker directly). 8. **Verify** — give me a test that starts the worker, sends SIGTERM mid-unit, and asserts the in-flight unit completed and locks were released. Output: (a) the worker loop with the trap + shutdown flag + interruptible sleep, (b) the EXIT cleanup that cooperates with it, (c) systemd/container config snippet, (d) the SIGTERM mid-unit test. Bias toward finishing the current unit cleanly and exiting before the grace period over abrupt exit.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Graceful Shutdown and Signal Handling Prompt
Add robust SIGTERM/SIGINT handling to a long-running Python worker so it finishes in-flight work, releases resources, and exits cleanly within the container's grace period instead of being SIGKILLed.
-
Systemd Timer Job Script Prompt
Convert a cron job into a robust systemd service + timer with proper logging, failure handling, sandboxing, and overlap prevention — or generate the units and accompanying script from scratch.
-
Bash Health-Gated Service Restart Orchestrator Prompt
Build a Bash script that restarts services one at a time, waits for each to pass a health check before moving on, and rolls back or aborts on failure to avoid taking a whole fleet down at once
-
Bash Cron-to-systemd-Timer Migration Prompt
Convert legacy crontab entries into equivalent systemd service+timer units with correct scheduling, logging, environment, and failure handling so scheduled jobs become observable and manageable.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.