Skip to main content

Claude/ChatGPT Prompt to Write a Bash Retry-with-Backoff Wrapper

Generate a reusable bash retry() function with exponential backoff, jitter, per-attempt timeout and clean logs, plus a real curl usage example.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model a senior systems engineer and asks for a reusable retry-with-backoff wrapper in bash, safe to drop into production scripts and CI. You set [command_type], [shell_target], and [max_attempts], and it returns a retry() function taking max attempts, base delay, and max delay; exponential backoff with jitter; respect for the wrapped command's exit code; an optional per-attempt timeout; clear per-attempt logging to stderr; and safety under set -euo pipefail.

The structure works because deploy and bootstrap scripts fail for boring, transient reasons — a registry hiccup, a not-yet-ready service — and a blind retry loop just hides the real errors. Jitter avoids thundering-herd retries, the per-attempt timeout stops a hung command blocking forever, and respecting exit codes means it returns immediately on success. [command_type] shapes the usage example, [shell_target] keeps it compatible with your shell and OS, and [max_attempts] sets the default attempt count. The "which failures NOT to retry" note is as important as the retry logic itself: retrying a client error or an auth failure burns time and buries the real cause, so the prompt makes that distinction explicit rather than leaving you to discover it during an outage.

When to use it

  • Your deploy or CI scripts fail on transient hiccups and a blind loop hides real errors.
  • You want exponential backoff with jitter to avoid thundering-herd retries.
  • You need a per-attempt timeout so a hung command can't block the pipeline forever.
  • You want clean per-attempt logging to stderr but silence on the happy path.
  • You're wrapping a flaky HTTP call and want it retried only on genuine failure.
  • You want one wrapper that's safe under set -euo pipefail across your scripts.

Example output

Expect the retry() function itself, a usage example wrapping a flaky [command_type] call (such as a curl to an API), and a note on which failures you should NOT retry. The function logs each attempt — attempt N of M, the delay, the exit code — to stderr, stays quiet when the command succeeds first try, and returns the wrapped command's own exit code.

Pro tips

  • Set [command_type] to the real thing you're wrapping so the usage example matches your case.
  • Match [shell_target] to your environment; the prompt can avoid bash 5-only features where reasonable if you need broader compatibility.
  • Tune [max_attempts] to the failure you expect — transient network blips want a few tries, not twenty.
  • Read the "don't retry these" note carefully; retrying a 400 or an auth failure just wastes time and hides a real bug.
  • Keep the per-attempt timeout enabled so a hung upstream can't stall your whole pipeline.
  • Don't suppress the stderr logging — it's how you tell a transient retry from a failure that needs you.
  • Set base and max delay to sane values for your [command_type]; a registry pull tolerates longer backoff than a fast health check.
  • Drop the function into a shared helper sourced by your scripts, so every deploy gets the same retry behaviour instead of each one reinventing it.

Frequently Asked Questions

Why does it use jitter and not just fixed backoff?
Jitter randomizes the delay between attempts so many clients retrying at once don't synchronize into a thundering herd that hammers the upstream simultaneously. Combined with exponential backoff, it spreads retries out and gives a struggling service room to recover.
Will it retry failures that shouldn't be retried?
It retries only on a non-zero exit and returns immediately on success, and it includes a note on which failures you should NOT retry. Retrying a client error like a 400 or an auth failure wastes time and masks a real bug, so heed that note.
Does it work under set -euo pipefail?
Yes, the wrapper is written to be safe under `set -euo pipefail`, which is what makes it droppable into strict production scripts and CI. It also offers a per-attempt timeout so a hung command can't block the pipeline indefinitely.
Can it run in shells other than bash 5?
Set `[shell_target]` to your environment and the prompt avoids bash 5-only features where reasonable, so the function works in more constrained shells. Naming the target keeps the generated code compatible rather than assuming the newest bash.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Linux & Shell Scripting Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support