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 purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]