Skip to content

Instantly share code, notes, and snippets.

@sj26
Last active November 6, 2024 21:56
Show Gist options
  • Save sj26/88e1c6584397bb7c13bd11108a579746 to your computer and use it in GitHub Desktop.
Save sj26/88e1c6584397bb7c13bd11108a579746 to your computer and use it in GitHub Desktop.
Bash retry function

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to https://unlicense.org

# Retry a command up to a specific numer of times until it exits successfully,
# with exponential back off.
#
# $ retry 5 echo Hello
# Hello
#
# $ retry 5 false
# Retry 1/5 exited 1, retrying in 1 seconds...
# Retry 2/5 exited 1, retrying in 2 seconds...
# Retry 3/5 exited 1, retrying in 4 seconds...
# Retry 4/5 exited 1, retrying in 8 seconds...
# Retry 5/5 exited 1, no more retries left.
#
function retry {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** $count))
count=$(($count + 1))
if [ $count -lt $retries ]; then
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
@hunter-richardson
Copy link

hunter-richardson commented Oct 16, 2019

Translated to fish (using printf instead of echo) with use of humantime:

#!/usr/bin/fish
function retry -d 'Retry a command up to a specified number of times, or until it exits successfully'
  if test (count $argv) -lt 2; or string match -irqv '^[0-9]+$' $argv[1]
    printf 'Proper use: %s [number of attempts until giving up] [command to attempt]\n' (status function);
    return 23
  else
    set -l retries $argv[1]
    set -l command (string split -m 1 ' ' "$argv")[2];
      and printf 'eval %s\n' (printf '%s ' $command)
    for i in (seq $retries)
      eval $command
      set -l cmd_status $status
      if test $cmd_status -eq 0
        printf 'Attempt #%d of %d succeeded.\n' $i $retries
        return 0
      else if test $i -eq $retries
        set -l exit $status
        printf 'Final attempt #%d exited %d.\n' $i $cmd_status
        return $cmd_status
      else
        set -l wait (math "2 ^ ($i - 1)")
        printf 'Attempt #%d of %d exited %d, reattempting in %s...\n\n' $i $retries $cmd_status (math "1000 * $wait" | humantime)
        sleep $wait
      end
    end
  end
end

Available here (with colorized output!).

Examples:

$ retry a echo Hello
Proper use: retry [number of attempts before giving up] [command to attempt]
#  exit code 23

$ retry 5
Proper use: retry [number of attempts before giving up] [command to attempt]
#  exit code 23

$ retry 8 false
eval false
Attempt #1 of 8 exited 1, reattempting in 1s...
Attempt #2 of 8 exited 1, reattempting in 2s...
Attempt #3 of 8 exited 1, reattempting in 4s...
Attempt #4 of 8 exited 1, reattempting in 8s...
Attempt #5 of 8 exited 1, reattempting in 16s...
Attempt #6 of 8 exited 1, reattempting in 32s...
Attempt #7 of 8 exited 1, reattempting in 1m 4s...
Final attempt #8 exited 1.
#  exit code 1

$ retry 5 echo Hello
eval echo Hello
Hello
Attempt #1 of 5 succeeded.
#  exit code 0

$ retry 5 ping -n -1 -w 1 google.com
eval ping -n 1 -w 1 google.com

Pinging google.com [172.217.164.174] with 32 bytes of data:
Request timed out.

Ping statistics for 172.217.164.174:
    Packets: Send =1, Received = 0, Lost = 1 (100% loss),
Attempt #1 of 5 exited 1, reattempting in 1s...

Pinging google.com [172.217.164.174] with 32 bytes of data:
Reply from 172.217.164.174:  bytes=32 time=30ms TTL=55

Ping statistics for 172.217.164.174:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)
Approximate round trip times in milliseconds:
    Minimum = 30ms, Maximum = 30ms, Average = 30ms
Attempt #2 of 5 succeeded.
#  exit code 0

@ypid
Copy link

ypid commented Nov 17, 2019

Well done! I hope you don’t mind, I am using it in hashbang/aosp-build#8. I reworked it a bit so that it passes ShellCheck.

@vmagnan
Copy link

vmagnan commented May 27, 2021

Thanks, it's very useful and easy to use!

@hcharley
Copy link

hcharley commented May 4, 2022

This is the full bash file I use to be able to call the file with the same args as the function fwiw:

#!/bin/bash

function retry {
  local retries=$1
  shift

  local count=0
  until "$@"; do
    exit=$?
    wait=$((2 ** $count))
    count=$(($count + 1))
    if [ $count -lt $retries ]; then
      echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
      sleep $wait
    else
      echo "Retry $count/$retries exited $exit, no more retries left."
      return $exit
    fi
  done
  return 0
}

retry $@

@jakub-bochenski
Copy link

@hcharley just define the function in your .bashrc or whatnot and you will be able to call it directly

@albertvaka
Copy link

What's the license of this code?

@sj26
Copy link
Author

sj26 commented Sep 5, 2022

It didn't have one, but I'm happy to pop it under MIT, and have added a license file.

@cmedved
Copy link

cmedved commented Dec 8, 2022

Personally, I much preferred it without a license, because now I can't use it without copying that chunk of text into my script above the function... which is a bit excessive.

@sj26
Copy link
Author

sj26 commented Dec 8, 2022

Fair point, I don't really care about the license notice being preserved. Unlicensed.

@ypid
Copy link

ypid commented Dec 12, 2022

https://reuse.software/ comes to the rescue. You can drop that license file (at least for this small script probably the easiest) or let the reuse tool manage it. Than add something like this to the top of the file:

# SPDX-FileCopyrightText: 2016, 2018-2019 Jane Doe <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

@corneliusroot
Copy link

Excellent function. Used it in a watchdog script, helps avoid false alarms due to initial time-outs. Thanks for publishing this!

@domo141
Copy link

domo141 commented Dec 30, 2022

Perhaps localize $wait and $exit, too ?

@grugnog
Copy link

grugnog commented Sep 3, 2023

One improvement would be to log to stderr instead of stdout - e.g.:
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." >&2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment