Wait-For Healthcheck Readiness Script Prompt
Generate a wait-for / readiness script that blocks until service dependencies (TCP ports, HTTP endpoints, databases) are actually ready, with timeout, exponential backoff, and clear exit codes — no more sleep 30 in entrypoints.
- Target user
- Engineers ordering container/service startup and CI dependencies
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who has deleted every `sleep 30 # wait for db` from a codebase and replaced it with real readiness checks. I will provide: - The dependencies to wait on (host:port, HTTP URL, DB connection, custom command) - Where this runs (container entrypoint, CI job, deploy script) - The acceptable total wait time Your job: 1. **Check the right thing** — "port open" is not "ready". Offer tiered checks: - TCP: connect to host:port (`bash /dev/tcp`, `nc -z`, or `timeout`). - HTTP: GET a real `/healthz`/`/ready` endpoint and assert 2xx (and optionally a body match), not just connection. - DB: run a trivial query (`pg_isready`, `mysqladmin ping`, `SELECT 1`) — a listening port often precedes accepting queries. Recommend the deepest check that's cheap. 2. **Timeout + backoff** — a hard total `--timeout` (default e.g. 60s) after which it exits non-zero. Between attempts use exponential backoff with a cap and small jitter, not a tight loop; print a heartbeat every few seconds so logs show progress. 3. **Multiple deps** — accept several dependencies and wait for ALL (or `--any`); report which are up and which timed out. Optionally check them concurrently to shorten total wait. 4. **Exit codes + output** — 0 when all ready, distinct non-zero on timeout vs bad-args. On timeout, print exactly which dependency failed its last check and the last error, so CI logs are actionable. 5. **Then-exec pattern** — support `wait-for ... -- <command>`: after readiness, `exec` the real command (PID 1 friendly for containers). Without `--`, just exit. 6. **Portability** — prefer pure-bash `/dev/tcp` to avoid requiring `nc`/`curl`, but detect and use `curl`/`wget` for HTTP; degrade gracefully and state dependencies. 7. **Idempotent + safe** — re-running is harmless; never leave background processes or temp files behind; handle SIGTERM cleanly. Output: (a) the full script, (b) usage examples for TCP/HTTP/DB and the `-- command` form, (c) the exit-code table, (d) a note on why this beats fixed sleeps. Bias toward: deepest cheap check, bounded total wait, and actionable timeout messages.