Skip to main content
InterviewHubPRO
Section Overview
Q30

Question 30 of 50

Part 3: Advanced Text Processing & Production Internals (Senior / Lead)

60%
Bash & Shell Scripting (50 Qs)•Part 3: Advanced Text Processing & Production Internals (Senior / Lead)

How do you implement Exponential Backoff and Retries in Bash?

senior LevelCode ExampleData Structures

In Simple Words: Exponential backoff in Bash retries failing operations in a loop, doubling the sleep interval between attempts to avoid overwhelming downstream services.

Code Example

bash
retry_command() {
    local max_attempts=5
    local delay=1
    local attempt=1

    while [[ $attempt -le $max_attempts ]]; do
        if "$@"; then
            return 0
        fi
        echo "Attempt $attempt failed! Retrying in ${delay}s..." >&2
        sleep $delay
        delay=$((delay * 2)) # Double delay
        attempt=$((attempt + 1))
    done
    echo "Command failed after $max_attempts attempts!" >&2
    return 1
}

retry_command curl -f https://api.com/healthcheck