-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwait-for-cmd.sh
More file actions
executable file
·88 lines (78 loc) · 1.78 KB
/
wait-for-cmd.sh
File metadata and controls
executable file
·88 lines (78 loc) · 1.78 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
#!/bin/bash -e
usage () {
echo "usage: $0 [-erv] [-i interval] [-t timeout] cmd [args ..]" >&2
exit 1
}
interval=5
timeout=60
verbose=0
redirerr=0
outputremaining=0
while getopts ":ei:rt:v" opt; do
case "${opt}" in
(e)
redirerr=1
;;
(i)
interval=$OPTARG
;;
(r)
outputremaining=1
;;
(t)
timeout=$OPTARG
;;
(v)
verbose=1
;;
(*)
usage
;;
esac
done
shift $((OPTIND-1))
cmd="$*"
if (( outputremaining && verbose )); then
echo "output remaining and verbose are mutually exclusive!" >&2
exit 1
fi
if [[ -z "$cmd" ]]; then
usage
fi
now () {
date +%s
}
base=$(now)
until=$(($base + $timeout))
remaining=$(($until - $(now)))
while true; do
if (( $verbose == 1 )); then
if bash -c "$cmd"; then
echo "\"$cmd\" succeeded in $(($(now) - $base))s of ${timeout}s" >&2
break
fi
elif (( $redirerr == 1 )); then
if bash -c "$cmd" 1>&2; then
echo "\"$cmd\" succeeded in $(($(now) - $base))s of ${timeout}s" >&2
break
fi
else
if bash -c "$cmd" > /dev/null 2> /dev/null; then
echo "\"$cmd\" succeeded in $(($(now) - $base))s of ${timeout}s" >&2
break
fi
fi
remaining=$(($until - $(now)))
if (( remaining <= 0 )); then
echo "\"$cmd\" not ready, failing after $(($(now) - $base))s" >&2
if (( $outputremaining )); then
echo 0
fi
exit 1
fi
echo "\"$cmd\" not yet ready in $(($(now) - $base))s of ${timeout}s, retrying in ${interval}s" >&2
sleep $interval
done
if (( $outputremaining )); then
echo $remaining
fi