Skip to content

Instantly share code, notes, and snippets.

@txominpelu
Forked from florianleibert/retry.bash
Last active August 29, 2015 14:07
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 txominpelu/a8e886cf55e2c26d55c8 to your computer and use it in GitHub Desktop.
Save txominpelu/a8e886cf55e2c26d55c8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -o nounset
set -o errexit
set -o pipefail
declare -r max=2
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 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