change wait-for-multiply.sh

This commit is contained in:
2026-03-27 16:51:12 +06:00
parent ce9e9a2d26
commit eec582c66b

View File

@@ -1,54 +1,49 @@
#!/usr/bin/env bash
# wait-for-multiple.sh
#!/bin/sh
# wait-for-multiple.sh (POSIX / sh version)
set -e
# Список хостов передается через аргументы до --
# Команда после --
HOSTS=()
CMD=()
while [[ $# -gt 0 ]]; do
HOSTS=""
CMD=""
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
CMD=("$@")
CMD="$*"
break
;;
*)
HOSTS+=("$1")
HOSTS="$HOSTS $1"
shift
;;
esac
done
if [[ ${#HOSTS[@]} -eq 0 ]]; then
if [ -z "$HOSTS" ]; then
echo "Usage: $0 host1:port1 host2:port2 ... -- command args"
exit 1
fi
# Функция для wait-for-it
wait_host() {
local hostport=$1
hostport="$1"
/usr/bin/wait-for-it.sh "$hostport" --timeout=30 --strict
}
# Запускаем все ожидания параллельно
PIDS=()
for h in "${HOSTS[@]}"; do
PIDS=""
for h in $HOSTS; do
wait_host "$h" &
PIDS+=($!)
PIDS="$PIDS $!"
done
# Ждем всех
FAIL=0
for pid in "${PIDS[@]}"; do
for pid in $PIDS; do
wait $pid || FAIL=1
done
if [[ $FAIL -ne 0 ]]; then
if [ $FAIL -ne 0 ]; then
echo "One or more hosts failed to become available"
exit 1
fi
# Запускаем команду после всех
exec "${CMD[@]}"
exec $CMD