-
Notifications
You must be signed in to change notification settings - Fork 43
/
template.sh
75 lines (66 loc) · 1.74 KB
/
template.sh
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
# shellcheck shell=bash
source __argShellCommands__
function _kill_port {
local pids
local port="${1}"
pids="$(mktemp)" \
&& if ! lsof -t "-i:${port}" > "${pids}"; then
info "No process was found listening on port ${port}" \
&& return 0
fi \
&& while read -r pid; do
if kill -9 "${pid}"; then
if timeout 5 tail --pid="${pid}" -f /dev/null; then
info "Process listening on port ${port} with PID ${pid} was successfully killed"
else
warn "Timeout while attempting to kill process with PID ${pid} listening on port ${port}"
fi
else
error "Unable to kill process with PID ${pid} listening on port ${port}"
fi
done < "${pids}"
}
function kill_port {
for port in "${@}"; do
_kill_port "${port}" \
|| return 1
done
}
function done_port {
local host="${1}"
local port="${2}"
kill_port "${port}" \
&& info "Done at ${host}:${port}" \
&& nc -kl "${host}" "${port}"
}
function wait_for_tcp {
local elapsed=1
local max_timeout=$(("${1}"))
local host="${2%:*}"
local port="${2#*:}"
while true; do
if timeout 1s nc -z "${host}" "${port}"; then
return 0
elif [ $elapsed -gt $max_timeout ]; then
error "Timeout while waiting for ${host}:${port} to open" \
&& return 1
else
info "Waiting 1 second for ${host}:${port} to open: ${elapsed} seconds in total" \
&& sleep 1 \
&& ((elapsed++)) \
&& continue
fi
done
}
function wait_port {
local pids=()
local max_timeout="${1}"
for address in "${@:2}"; do
{ wait_for_tcp "${max_timeout}" "${address}" & } \
&& pids+=("${!}")
done \
&& for pid in "${pids[@]}"; do
wait "${pid}" \
|| return 1
done
}