#!/usr/bin/env bash # wait-for-multiply.sh # Wait for multiple TCP hosts/ports in parallel before executing a command set -e WAIT_FOR_SCRIPT="/usr/bin/wait-for-it.sh" HOSTS=() CMD=() # Parse arguments: everything until -- is host:port, rest is command while [[ $# -gt 0 ]]; do case "$1" in --) shift CMD=("$@") break ;; *) HOSTS+=("$1") shift ;; esac done if [[ ${#HOSTS[@]} -eq 0 ]]; then echo "Error: specify at least one host:port" exit 1 fi if [[ ${#CMD[@]} -eq 0 ]]; then echo "Error: specify command to run after hosts are ready" exit 1 fi # Function to wait for a single host wait_one() { local hostport=$1 "$WAIT_FOR_SCRIPT" "$hostport" --strict } # Start all waits in parallel PIDS=() for HOSTPORT in "${HOSTS[@]}"; do wait_one "$HOSTPORT" & PIDS+=($!) done # Wait for all to finish and check exit codes EXIT_CODE=0 for PID in "${PIDS[@]}"; do wait $PID || EXIT_CODE=$? done if [[ $EXIT_CODE -ne 0 ]]; then echo "Error: One or more services failed to become available" exit $EXIT_CODE fi # Execute the final command exec "${CMD[@]}"