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
}
@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