Wait until a service is up

Retry until a TCP port answers, with a timeout.

bash intermediate #docker#deploy#networking
bash-wait-for-port.sh
wait_for() {
  local host="$1" port="$2" timeout="${3:-30}"
  for ((i = 0; i < timeout; i++)); do
    if (exec 3<>"/dev/tcp/$host/$port") 2>/dev/null; then
      echo "$host:$port is up"; return 0
    fi
    sleep 1
  done
  echo "timed out waiting for $host:$port" >&2; return 1
}

wait_for localhost 5432 60 && flask db upgrade