Created
November 2, 2012 08:40
-
-
Save florianleibert/3999522 to your computer and use it in GitHub Desktop.
Retry logic in bash (for ssh / scp)
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
#!/bin/bash | |
set -o nounset | |
set -o errexit | |
set -o pipefail | |
declare -r max=4 | |
declare i=0 | |
function wrap() { | |
local cmd=$1 ; shift | |
retry $cmd "$@" | |
set -o errexit | |
} | |
function retry() { | |
set +o errexit | |
local cmd=$1 ; shift | |
$cmd "$@" | |
if [ $? -ne 0 -a $i -lt $max ] ; then | |
i=$(($i+1)) | |
echo "Retrying" | |
sleep $((1+$i*$i*5)) | |
retry $cmd "$@" | |
fi | |
} | |
wrap "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful little script.
I've made changes based upon both of the above, so if it fails with Code 6 initially (missing local file), it'll not continue retrying, and it'll similarly pass out any other non-zero failure codes after the appropriate retry length.
https://gist.github.com/leezer3/7f5add40ddb7b16654d2378e292bb28b