parent
31ab43ce41
commit
ff1a7a81a7
2 changed files with 28 additions and 2 deletions
@ -0,0 +1,23 @@ |
|||||||
|
#!/bin/bash |
||||||
|
|
||||||
|
function fail { |
||||||
|
echo $1 >&2 |
||||||
|
exit 1 |
||||||
|
} |
||||||
|
|
||||||
|
function retry { |
||||||
|
local n=1 |
||||||
|
local max=3 # 3 retries before failure |
||||||
|
local delay=5 # delay between retries, 5 seconds |
||||||
|
while true; do |
||||||
|
echo "Running command '$@' with retry, attempt $n/$max" |
||||||
|
"$@" && break || { |
||||||
|
if [[ $n -lt $max ]]; then |
||||||
|
((n++)) |
||||||
|
sleep $delay; |
||||||
|
else |
||||||
|
fail "The command has failed after $n attempts." |
||||||
|
fi |
||||||
|
} |
||||||
|
done |
||||||
|
} |
Loading…
Reference in new issue