Skip to content

Instantly share code, notes, and snippets.

@leezer3
Forked from florianleibert/retry.bash
Last active March 20, 2017 14:15
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 leezer3/7f5add40ddb7b16654d2378e292bb28b to your computer and use it in GitHub Desktop.
Save leezer3/7f5add40ddb7b16654d2378e292bb28b to your computer and use it in GitHub Desktop.
Retry logic in bash (for ssh / scp)
#!/usr/bin/env 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 "$@"
local success=$?
set -o errexit
exit $success
}
function retry() {
set +o errexit
local cmd=$1 ; shift
$cmd "$@"
s=$?
if [ $s -ne 6 ] ;
then
return $s
elif [ $s -ne 0 -a $i -lt $max ] ;
then
i=$(($i+1))
echo "Retrying"
sleep $((1+$i*$i*5))
retry $cmd "$@";
else
return $s
fi
}
wrap "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment