Bash Hung-Process Guard with timeout Prompt
Wrap flaky external commands (curl, ssh, database clients, cloud CLIs) in a Bash timeout/kill guard so a single hung call can never stall a cron job, CI step, or systemd task forever.
- Target user
- DevOps engineers whose automation occasionally hangs on a network call and blocks the whole pipeline
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior reliability-focused shell engineer. I keep getting scripts that hang indefinitely on a slow network call, and the whole cron/CI job stalls until something kills it. I want you to add a robust hung-process guard. I will provide: - The script or the specific command(s) that can hang (e.g. `curl`, `ssh`, `psql`, `aws`, `kubectl`) - Where it runs (cron, systemd service/timer, CI runner, operator laptop) - Acceptable wall-clock budget per command and for the whole script - Whether the command is safe to retry (idempotent) or must run exactly once Produce a hardened version and explain every decision: 1. **Wrap with `timeout` correctly** — use `timeout --signal=TERM --kill-after=10s <duration> <cmd>` so a stuck process first gets a chance to clean up (SIGTERM), then is force-killed (SIGKILL) if it ignores it. Explain why `--kill-after` matters and what happens without it. 2. **Distinguish timeout from real failure** — `timeout` exits `124` when it fires and `137` when the `--kill-after` SIGKILL lands. Show a `case "$?" in` block that logs "timed out" vs the command's own non-zero exit distinctly, because they demand different responses. 3. **Process-group kill** — explain when to use `timeout -k` alone vs `timeout` plus running the command in its own group, so child processes (a `curl` spawned by a wrapper, an ssh multiplexed session) don't survive as orphans. Show `setsid`/process-group handling if children leak. 4. **Bounded retries with backoff** — only for idempotent commands, wrap the guarded call in a retry loop with exponential backoff and a jitter, capped by an overall deadline so retries can't exceed the script's total budget. 5. **Overall script deadline** — add a top-level watchdog (a background `sleep N && kill -TERM $$` or a `SECONDS`-based check) so even un-guarded sections cannot run past the total budget. 6. **Observability** — log start time, the timeout budget, elapsed seconds, and the classification (ok / timeout / error) in a single structured line per attempt so failures are greppable. Output: (a) the full annotated script with strict mode (`set -Eeuo pipefail`), (b) a table mapping exit codes 0/124/137/other to meaning and action, (c) a short note on the `timeout` GNU-vs-BSD differences and how to detect which is installed. Bias toward: never hang forever, always classify why a command ended, never force-kill a non-idempotent command without saying so.
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
-
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
-
Python signal.alarm SIGALRM Timeout Watchdog Prompt
Wrap stubborn blocking calls with a SIGALRM-based timeout context manager, fully aware of its main-thread and Unix-only caveats.
-
Bash Multi-Repo Git Bulk Command Runner Prompt
Build a Bash script that runs the same git or shell command across many local repositories with per-repo isolation, dirty-tree guards, and an aggregated pass/fail summary
-
Bash Safe In-Place sed Edit Wrapper Prompt
Build a Bash wrapper around sed -i that backs up, validates, and atomically applies in-place edits so a bad expression can never corrupt or truncate config files
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.