Task 06 — Retry with exponential backoff
Write a wrapper script 06.sh that runs a command, and on failure retries with exponentially increasing sleeps until success or max attempts.
Submit as
06.sh
Input
| Source | Format |
|---|---|
| CLI | ./06.sh <max_attempts> <initial_delay_sec> -- <command> [args...] |
<max_attempts> — positive integer, total tries (including the first) |
|
<initial_delay_sec> — positive integer, seconds to sleep after the first failure |
|
-- — separator; everything after it is the command to run (may contain spaces/flags) |
Output
| Stream | Format |
|---|---|
| stdout | Not checked (your script may forward the child's stdout; grader redirects away) |
| stderr | Not checked |
| Exit code | 0 if any attempt succeeds |
| Otherwise the exit code of the last failed attempt (propagate the child's code) | |
| Timing | After a failure: sleep delay seconds, then set delay = delay * 2 for the next failure. Do not sleep after a successful attempt. |
Example
# Up to 5 attempts; sleeps 1s, then 2s, then 4s, then 8s between failures.
./06.sh 5 1 -- curl -fsSL https://api.example.com/health
Flaky command (fails twice, then succeeds):
./06.sh 3 1 -- /path/to/sometimes_works
echo $? # → 0
Always fails:
./06.sh 3 1 -- false
echo $? # → 1 (last attempt's exit code)
What the grader checks
| Case | Expected |
|---|---|
Command always exits 7 |
Script exits 7 after exactly 3 attempts (max_attempts=3) |
Fails twice, then 0 |
Script exits 0 after exactly 3 invocations |
| Succeeds on first try | Script exits 0 after 1 invocation, elapsed time ≤ 2s (no sleep) |
Hints
- Use
"$@"after parsing past-- command; rc=$?to capture exit codessleep "$delay"thendelay=$((delay * 2))