LinuxCommandLibrary

retry

Retry a command until it succeeds

TLDR

Retry a command until it succeeds

$ retry [command]
copy

Retry a command every n seconds until it succeeds
$ retry --delay=[n] [command]
copy

Give up after n attempts
$ retry --times=[n] [command]
copy

SYNOPSIS

retry [OPTIONS] COMMAND [ARGUMENTS...]
Example: retry -n 5 -d 2 curl -f http://example.com/data

PARAMETERS

-n N, --attempts N
    Specifies the maximum number of times to retry the `COMMAND`.

-d S, --delay S
    Sets the delay in seconds between consecutive retry attempts.

-q, --quiet
    Suppresses all output from `retry` itself, only showing `COMMAND` output.

-v, --verbose
    Enables verbose output, showing each retry attempt and its status.

-b, --backoff
    Applies an exponential backoff strategy to the delay between attempts.

-t S, --timeout S
    Sets a timeout in seconds for the `COMMAND` itself to complete.

-e CODES, --exit-codes CODES
    Defines a comma-separated list of exit codes considered successful.

DESCRIPTION

The retry command is a powerful utility designed to enhance the robustness of scripts and automated tasks by automatically re-executing a specified command until it succeeds or a predefined number of attempts is exhausted. This is particularly useful in environments where transient failures are common, such as unreliable network connections, temporary resource unavailability, or race conditions.

Instead of immediately failing when a command returns a non-zero exit code (indicating an error), retry allows for a configurable delay between attempts and a maximum retry count. This mechanism significantly improves the resilience of operations that might otherwise fail due to temporary glitches. Common scenarios include fetching data from a remote server, connecting to a database, or performing file operations on a network share. By incorporating retry, system administrators and developers can create more stable and fault-tolerant automation, reducing the need for manual intervention and improving overall system reliability. It typically monitors the exit status of the executed command, considering an exit code of 0 as success and any non-zero code as a failure requiring a retry.

CAVEATS

The retry command is not a standard POSIX utility and its implementation can vary significantly. Users should be aware that it's often a custom script or a third-party tool. Incorrect usage, especially with high retry counts or short delays, can lead to excessive resource consumption or even contribute to the very issues it's trying to mitigate (e.g., overwhelming a service). Ensure the retried command is idempotent if side effects are not desired on multiple executions.

IMPLEMENTATION NOTES

Most retry implementations work by executing the given `COMMAND` and inspecting its exit status. A zero exit code typically signifies success, while any non-zero code indicates an error. The utility then pauses for a specified `delay` and re-executes the command if the `attempts` limit has not been reached.

COMMON USE CASES

Connecting to a remote server: retry -n 10 -d 5 ssh user@host 'echo "Connected!"'
Downloading a file: retry -n 5 -d 10 curl -fL http://example.com/largefile -o localfile.zip
Waiting for a service: retry -n 30 -d 2 nc -z localhost 8080 (checking if port is open)

HISTORY

While not a single, universally standardized command with a formal development history, the concept of "retrying" operations has been a fundamental pattern in robust programming and scripting for decades. Its prominence in Linux environments grew with the increasing complexity of distributed systems and network-dependent applications. Shell scripts implementing retry functionality became commonplace as a practical way to handle transient failures without requiring complex error handling logic within the primary command itself. Various community-driven implementations and custom scripts evolved, reflecting a shared need for increased resilience in automated tasks. It represents a practical, decentralized solution to a pervasive problem in systems administration and automation.

SEE ALSO

sleep(1), watch(1), bash(1), sh(1), curl(1)

Copied to clipboard