#!/bin/bash
# $1 = host to connect to (e.g. localhost)
# $2 = port to connect to (e.g. 3306)
# $3 = timeout in seconds, after which the script will exit with status 1 (optional)
# Example usage:
#   wait-for-it.sh localhost:3306 -- /usr/bin/python app.py
#
set -x
host="${1%:*}"
port="${1#*:}"
shift
# shellcheck disable=SC2124
cmd="$@"
until nc -z "$host" "$port"; do
  echo >&2 "Waiting for $host:$port to be available..."
  sleep 1
done
echo >&2 "$host:$port is available!"
exec "$cmd"
