#!/usr/bin/env bash # wait-for-url.sh — curl with retry until success or attempts exhausted. # # Usage: wait-for-url.sh [] [] # Env (optional): BASIC_AUTH_USER, BASIC_AUTH_PASS — if set, sent as basic auth. set -euo pipefail URL="${1:-}" MAX_ATTEMPTS="${2:-30}" DELAY="${3:-2}" if [ -z "$URL" ]; then echo "usage: $0 [] []" >&2 exit 2 fi # bash 3.2-safe: expand array only when non-empty. AUTH_ARGS=() if [ -n "${BASIC_AUTH_USER:-}" ] && [ -n "${BASIC_AUTH_PASS:-}" ]; then AUTH_ARGS=(--user "${BASIC_AUTH_USER}:${BASIC_AUTH_PASS}") fi attempt=1 while [ "$attempt" -le "$MAX_ATTEMPTS" ]; do if curl -fsS ${AUTH_ARGS[@]+"${AUTH_ARGS[@]}"} -o /dev/null "$URL"; then echo "ok: $URL ($attempt attempt(s))" exit 0 fi if [ "$attempt" -lt "$MAX_ATTEMPTS" ]; then sleep "$DELAY" fi attempt=$((attempt + 1)) done echo "fail: $URL did not return 2xx after $MAX_ATTEMPTS attempts" >&2 exit 1