-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwait-for-it.sh
More file actions
executable file
·96 lines (82 loc) · 2 KB
/
Copy pathwait-for-it.sh
File metadata and controls
executable file
·96 lines (82 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
cmdname=$(basename "$0")
TIMEOUT=120
QUIET=0
echoerr() {
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
}
usage() {
exitcode="$1"
cat << USAGE >&2
Usage:
$cmdname host:port [-t timeout] [-- command args]
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit "$exitcode"
}
wait_for() {
local wait_host="$1"
local wait_port="$2"
local timeout="$3"
echoerr "Waiting for $wait_host:$wait_port..."
for i in $(seq 1 "$timeout"); do
echoerr "Checking: $wait_host:$wait_port - attempt $i"
if nc -z "$wait_host" "$wait_port" > /dev/null 2>&1; then
echoerr "$wait_host:$wait_port is available after $i seconds"
return 0
fi
sleep 1
done
echoerr "Operation timed out" >&2
return 1
}
# Extract host and port from input
if [ "$#" -lt 1 ]; then
usage 1
fi
if [[ "$1" != *:* ]]; then
echoerr "Invalid host:port format"
usage 1
fi
HOST=$(echo "$1" | cut -d : -f 1)
PORT=$(echo "$1" | cut -d : -f 2)
shift
# Debug output for host and port
echoerr "Host: $HOST"
echoerr "Port: $PORT"
# Process additional arguments
while [ $# -gt 0 ]; do
case "$1" in
-t)
TIMEOUT="$2"
if [ -z "$TIMEOUT" ]; then
echoerr "Timeout value is required after -t"
usage 1
fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
break
;;
--help)
usage 0
;;
*)
echoerr "Unknown argument: $1"
usage 1
;;
esac
done
# Debug output for timeout value
echoerr "Timeout: $TIMEOUT"
wait_for "$HOST" "$PORT" "$TIMEOUT" || exit 1
if [ $# -gt 0 ]; then
exec "$@"
fi