Skip to content

Instantly share code, notes, and snippets.

@NorseGaud
Created August 2, 2023 17:44
Show Gist options
  • Save NorseGaud/c0615901889dc4d2ca431ff68933da2c to your computer and use it in GitHub Desktop.
Save NorseGaud/c0615901889dc4d2ca431ff68933da2c to your computer and use it in GitHub Desktop.
BASH script: exponential backoff with limit of 60s
set -E # required to get exit code from while ()
BASE=${BACKOFF_BASE:-1}
MAX=${BACKOFF_MAX:-60}
FAILURES=0
while (
# Code here
) 2>&1; RC=$?; [[ $RC -ne 0 ]]; do
FAILURES=$(( $FAILURES + 1 ))
SECONDS=$(( ($BASE * 2) ** ($FAILURES - 1) ))
if [[ $SECONDS -ge $MAX ]]; then
SECONDS=$MAX
FAILURES=$(( $FAILURES - 1 )) # make sure SECONDS doesn't grow too large
fi
print_warning "$FAILURES failure(s), retrying in $SECONDS second(s)" >&2
sleep $SECONDS
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment