-
Notifications
You must be signed in to change notification settings - Fork 1
/
with_retries
executable file
·31 lines (25 loc) · 889 Bytes
/
with_retries
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
#!/bin/bash
# Modified version of travis_retry
# https://github.com/travis-ci/travis-build/blob/04a1963de816b8618bb3d7e2e22d873f05ab4091/lib/travis/build/bash/travis_retry.bash
SLEEP_SECONDS=${SLEEP_SECONDS:-9}
RETRY_COUNT=${RETRY_COUNT:-20}
retry_command() {
local result=0
local count=1
while [[ "${count}" -le $RETRY_COUNT ]]; do
[[ "${result}" -ne 0 ]] && {
echo -e "\\n${ANSI_RED}The command \"${*}\" failed. Retrying, ${count} of ${RETRY_COUNT}.${ANSI_RESET}\\n" >&2
}
# run the command in a way that doesn't disable setting `errexit`
"${@}"
result="${?}"
if [[ $result -eq 0 ]]; then break; fi
count="$((count + 1))"
sleep $SLEEP_SECONDS
done
[[ "${count}" -gt ${RETRY_COUNT} ]] && {
echo -e "\\n${ANSI_RED}The command \"${*}\" failed ${RETRY_COUNT} times.${ANSI_RESET}\\n" >&2
}
return "${result}"
}
retry_command "$@"