>_ IzzyLab

Hands-on task

This task is step 26 of the course “Bash scripts: from a loop to a robust script”

Retry with increasing delays

hard~12 min

The command /usr/local/bin/flaky.sh is unreliable: the first calls fail, and from some attempt on it succeeds. Write /root/retry.sh that calls it repeatedly up to five times, pausing between attempts with a delay that doubles each time (1, 2, 4, …). The script must exit with code 0 as soon as the command succeeds, and with a non-zero code if all attempts are used up. Use the sleep command for the pauses. Make the script executable.

What you'll practise:
  • retry
  • exponential backoff
  • exit codes
  • arithmetic expansion
  • loops

Topic breakdown

Retrying with increasing delays (exponential backoff) is the standard way to survive transient failures: network issues, an unavailable service, a concurrent lock. The point of growing the delay is not to hammer an overloaded system at the same rate and to give it time to recover, while limiting the number of attempts guarantees the script won't loop forever. Success is determined by the exit code (if command; then), not by parsing output. Real implementations add random spread (jitter) to the scheme so that clients that failed at the same time don't come back in sync.

What's checked

  1. the script succeeds
  2. retries continue until the first success and no further
  3. the script doesn't loop forever
  4. if all attempts fail, the script gives up with an error
  5. the number of attempts matches the task
  6. doubling delay between attempts

What it looks like