Created
February 24, 2011 16:41
-
-
Save dansimau/842415 to your computer and use it in GitHub Desktop.
Bash function for running a command, checking the return code, and re-trying it x times after y sleep seconds.
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
# | |
# "<cmd>" <retry times> <retry wait> | |
# | |
do_retry() | |
{ | |
cmd="$1" | |
retry_times=$2 | |
retry_wait=$3 | |
c=0 | |
while [ $c -lt $((retry_times+1)) ]; do | |
c=$((c+1)) | |
echo "Executing \"$cmd\", try $c" | |
$1 && return $? | |
if [ ! $c -eq $retry_times ]; then | |
echo "Command failed, will retry in $retry_wait secs" | |
sleep $retry_wait | |
else | |
echo "Command failed, giving up." | |
return 1 | |
fi | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eg.:
Some script:
Result: