Code Reference

Healthcheck Loop

Bash · Example / how-to

Healthcheck Loop

Bash · Example / how-to


📋 Overview

Poll an HTTP health endpoint until it succeeds or a timeout expires — useful for deploy wait scripts.

🔧 Core concepts

PieceRole
Loop + sleepRetry with backoff gap
curl -fsSFail on HTTP errors
DeadlineAbsolute timeout
Exit codes0 healthy, 1 timed out

💡 Examples

healthcheck_loop.sh:

#!/usr/bin/env bash
set -euo pipefail

URL="${1:-http://127.0.0.1:8000/health}"
TIMEOUT_SEC="${TIMEOUT_SEC:-60}"
INTERVAL_SEC="${INTERVAL_SEC:-2}"
START="$(date +%s)"

echo "waiting for $URL (timeout ${TIMEOUT_SEC}s)"

while true; do
  if curl -fsS --max-time 5 "$URL" >/dev/null; then
    echo "healthy"
    exit 0
  fi

  NOW="$(date +%s)"
  ELAPSED="$((NOW - START))"
  if ((ELAPSED >= TIMEOUT_SEC)); then
    echo "error: timed out after ${TIMEOUT_SEC}s" >&2
    exit 1
  fi

  echo "not ready (${ELAPSED}s)…"
  sleep "$INTERVAL_SEC"
done

Usage:

chmod +x healthcheck_loop.sh
./healthcheck_loop.sh http://127.0.0.1:9000/healthz
TIMEOUT_SEC=120 INTERVAL_SEC=3 ./healthcheck_loop.sh https://example.com/health

⚠️ Pitfalls

  • curl without -f treats 500 as success — always use -f or check status.
  • Infinite loops without a deadline hang CI jobs.
  • DNS/TLS failures need clear stderr; avoid swallowing curl errors silently.

On this page