Skip to content

Instantly share code, notes, and snippets.

@mandric
Last active January 7, 2020 16:43
Show Gist options
  • Save mandric/4bedb2f82ddef1d0b7950227393b481f to your computer and use it in GitHub Desktop.
Save mandric/4bedb2f82ddef1d0b7950227393b481f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# https://github.com/microsoft/dotnet-framework-docker/blob/master/eng/common/pull-image.sh
# Stop script on NZEC
set -e
# Stop script if unbound variable found (use ${var:-} if intentional)
set -u
print_err() {
printf "%b\n" "Error: $1" >&2
}
# Executes a command and retries if it fails.
retry() {
local count=0
local retries=5
local waitFactor=6
until "$@"; do
local exit=$?
count=$(( $count + 1 ))
if [ $count -lt $retries ]; then
local wait=$(( waitFactor ** (( count - 1 )) ))
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
print_err "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment