Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Last active April 7, 2017 05:21
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 toolmantim/8313dc5ededd66b88077aeaa0c71f593 to your computer and use it in GitHub Desktop.
Save toolmantim/8313dc5ededd66b88077aeaa0c71f593 to your computer and use it in GitHub Desktop.
A Bash function you can use in scripts to retry Buildkite steps. License: MIT
#!/bin/bash
# Retries a command a number of times, with exponential backoff
#
# Usage:
# retry <number-of-retries> <command>
#
# Examples:
# retry 3 make test
# retry 5 curl -O https://buildkite.com/
function retry {
local retries=$1; shift
local attempts=1
local status
until "$@"; do
status=$?
if (( attempts == retries )); then
echo "Failed $attempts retries"
return 1
else
echo "Retrying $((retries - attempts)) more times..."
attempts=$((attempts + 1))
sleep $(((attempts - 2) * 2))
fi
done
return $status
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment