Modern QA2026Practical Script: Wait for Service Readiness — tiles
Log inJoin
36 / 55 · 19 Linux & Command Line · Bash Scripting for QA← prev⊞ allnext →☰ Read as one page

5.6Practical Script: Wait for Service Readiness

#!/bin/bash
# wait-for-service.sh -- Wait until a service is ready before running tests
set -euo pipefail

URL="${1:?Usage: $0 <url> [timeout_seconds]}"
TIMEOUT="${2:-120}"
INTERVAL=5
ELAPSED=0

echo "Waiting for $URL to be ready (timeout: ${TIMEOUT}s)..."

while [ "$ELAPSED" -lt "$TIMEOUT" ]; do
    STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$URL" 2>/dev/null || echo "000")

    if [ "$STATUS" -eq 200 ]; then
        echo "Service is ready (HTTP $STATUS) after ${ELAPSED}s"
        exit 0
    fi

    echo "  Not ready (HTTP $STATUS), retrying in ${INTERVAL}s... (${ELAPSED}/${TIMEOUT}s)"
    sleep "$INTERVAL"
    ELAPSED=$((ELAPSED + INTERVAL))
done

echo "ERROR: Service not ready after ${TIMEOUT}s"
exit 1

Usage: ./wait-for-service.sh https://staging.example.com/health 180