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 1

Preparing 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

← All Scripting (Bash, Groovy) questions