Created
March 30, 2023 10:15
-
-
Save fieg/1ae4232760ae68ccd354e9d4af717ad3 to your computer and use it in GitHub Desktop.
Bash function to wait for a url to be available with a fibonacci backoff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Function to wait for a url to be available with a fibonacci backoff | |
# | |
# Usage: | |
# wait_for_it <url> | |
# | |
# Examples: | |
# wait_for_it "http://some-service:port/path/" | |
# wait_for_it "google.com" | |
# wait_for_it "$1" | |
function wait_for_it() { | |
local url=${1} | |
local backoff=(1 1 2 3 5 8 13 21) | |
local attempt=0 | |
until curl --output /dev/null --silent --head --fail "${url}"; do | |
local wait=${backoff[(attempt++)]:-34} | |
echo "waiting for ${url}, retry in ${wait}s"; sleep "${wait}" | |
done | |
} | |
wait_for_it "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment