Interview › Scripting (Bash, Groovy)
How would you write a health-check script that polls a URL until it returns 200? [Advanced]
Answer
I would poll the URL with curl, require HTTP 200, sleep between attempts, and enforce a timeout or maximum attempts. The script should print diagnostics and exit nonzero if the service never becomes healthy.
Technical explanation
Use curl -fsS to fail on HTTP errors, show errors, and stay quiet on success.
Use --max-time to prevent one request from hanging forever.
For Kubernetes rollouts, prefer native readiness and kubectl rollout status, but URL polling is useful for external checks.
Hands-on example
#!/usr/bin/env bash
set -euo pipefail
url=${1:?url required}
for attempt in {1..30}; do
if code=$(curl -sS -o /dev/null -w '%{http_code}' --max-time 3 "$url") && [[ "$code" == "200" ]]; then
echo "healthy"
exit 0
fi
echo "waiting for 200, got ${code:-curl-error}" >&2
sleep 2
done
echo "service did not become healthy: $url" >&2
exit 1Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is the difference between a cron job and a systemd timer? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- What is the difference between sed -i and sed without -i? [Advanced]
- How do you use awk to sum or average a column? [Advanced]
- How do you define and call a function (or step) in a Jenkins Groovy pipeline? [Advanced]
- What is the difference between NR and NF in awk? [Advanced]
- How do you handle errors and retries in a Groovy pipeline (try/catch, retry, timeout)? [Advanced]