Created
January 22, 2014 10:47
-
-
Save ViktorStiskala/8556763 to your computer and use it in GitHub Desktop.
Bash retry function with max_retries settings
This file contains hidden or 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
#!/bin/bash | |
number=$RANDOM | |
let "number %= 10" | |
if [[ $number -gt 8 ]] | |
then | |
echo "Success" | |
exit 0 | |
fi | |
echo "Fail" | |
exit 1 |
This file contains hidden or 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
#!/bin/bash | |
retry () { | |
local max_retries=$1 | |
shift 1 | |
local retry_counter=$max_retries | |
while [ $retry_counter -gt 0 ]; | |
do | |
if [ $retry_counter -ne $max_retries ] | |
then | |
echo "Retrying: $@" 1>&2 | |
fi | |
"$@" && break | |
retry_counter=$(($retry_counter - 1)) | |
done | |
if [ $retry_counter -eq 0 ] | |
then | |
echo "Max retries reached, giving up" 1>&2 | |
return 1 | |
fi | |
return 0 | |
} | |
retry 5 ./random_crash.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment