Last active
October 26, 2023 15:27
-
-
Save aslafy-z/06ea16b6d30ad1d8497158aea9be74df to your computer and use it in GitHub Desktop.
Bash Retry helper that supports stabilization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function _retry () ( | |
if [ $# -lt 1 ]; then | |
echo >&2 "usage: $0 <command> [max] [min] [step] [timeout]" | |
return | |
fi | |
cmd=$1 | |
max=${2:-10} | |
min=${3:-1} | |
step=${4:-5} | |
timeout=${5:-0} | |
failed=0 | |
succeeded=0 | |
set +e | |
sigint=0 | |
trap 'sigint=1' SIGINT | |
while true; do | |
eval "(timeout $timeout "$cmd") >&2 &" | |
child=$! | |
wait "$child" | |
ret=$? | |
if [ "$sigint" = 1 ]; then | |
kill -SIGINT "$child" | |
wait "$child" | |
echo | |
echo "retry: warning: stopping execution because of SIGINT signal" | |
return 1 | |
fi | |
if [ "$ret" = 0 ]; then | |
((succeeded=succeeded+1)) | |
if [ "$succeeded" -ge "$min" ]; then | |
echo >&2 "retry: success: command '$cmd' is stable" | |
return 0 | |
else | |
echo >&2 "retry: command '$cmd' succeeded, stabilizating [$succeeded/$min]" | |
fi | |
((failed=0)) | |
else | |
((failed=failed+1)) | |
if [ "$failed" -ge "$max" ]; then | |
echo >&2 "retry: error: command '$cmd' failed after $failed tries" | |
return 1 | |
fi | |
echo >&2 "retry: command '$cmd' failed, retrying [$failed/$max]" | |
((succeeded=0)) | |
fi | |
sleep "$step" | |
[ "$sigint" = 1 ] && return 1 | |
done | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment