Skip to content

Instantly share code, notes, and snippets.

@aslafy-z
Last active October 26, 2023 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslafy-z/06ea16b6d30ad1d8497158aea9be74df to your computer and use it in GitHub Desktop.
Save aslafy-z/06ea16b6d30ad1d8497158aea9be74df to your computer and use it in GitHub Desktop.
Bash Retry helper that supports stabilization
#!/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