Skip to content

Instantly share code, notes, and snippets.

@nathforge
Created December 22, 2016 10:33
Show Gist options
  • Save nathforge/62456d9b18e276954f58eb61bf234c17 to your computer and use it in GitHub Desktop.
Save nathforge/62456d9b18e276954f58eb61bf234c17 to your computer and use it in GitHub Desktop.
Exponential backoff in Bash
expbackoff() {
# Exponential backoff: retries a command upon failure, scaling up the delay between retries.
# Example: "expbackoff my_command --with --some --args --maybe"
local MAX_RETRIES=${EXPBACKOFF_MAX_RETRIES:-8} # Max number of retries
local BASE=${EXPBACKOFF_BASE:-1} # Base value for backoff calculation
local MAX=${EXPBACKOFF_MAX:-300} # Max value for backoff calculation
local FAILURES=0
while ! "$@"; do
FAILURES=$(( $FAILURES + 1 ))
if (( $FAILURES > $MAX_RETRIES )); then
echo "$@" >&2
echo " * Failed, max retries exceeded" >&2
return 1
else
local SECONDS=$(( $BASE * 2 ** ($FAILURES - 1) ))
if (( $SECONDS > $MAX )); then
SECONDS=$MAX
fi
echo "$@" >&2
echo " * $FAILURES failure(s), retrying in $SECONDS second(s)" >&2
sleep $SECONDS
echo
fi
done
}
something_that_succeeds() { echo "I'm a winner!"; }
something_that_fails() { echo "I'm a loser :("; return 1; }
EXPBACKOFF_MAX_RETRIES=3 # Override default value - speeds up testing
expbackoff something_that_succeeds --calling it with -args
echo # Clear up the display
expbackoff something_that_fails --calling it with -args
echo
echo $? # Should be 1, indicating overall failure of `something_that_fails`
@steven-shi-hs
Copy link

steven-shi-hs commented May 1, 2017

Great! How about if making a little
if (( $SECONDS > $MAX )); then SECONDS=$MAX echo "$@" >&2 echo " * Failed, max timeout exceeded" >&2 return 1 fi

@ErikMeinders
Copy link

ErikMeinders commented Jan 16, 2019

if you remove line 34, you're right. $? is return code of last command. echo hardly ever fails ... echo (line 34) succeeds, echo $? (35) will display 0, meaning echo (34) went well.

$ false
$ echo $?
1
$ echo $?
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment