Skip to content

Instantly share code, notes, and snippets.

@ViktorStiskala
Created January 22, 2014 10:47
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 ViktorStiskala/8556763 to your computer and use it in GitHub Desktop.
Save ViktorStiskala/8556763 to your computer and use it in GitHub Desktop.
Bash retry function with max_retries settings
#!/bin/bash
number=$RANDOM
let "number %= 10"
if [[ $number -gt 8 ]]
then
echo "Success"
exit 0
fi
echo "Fail"
exit 1
#!/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