Skip to content

Instantly share code, notes, and snippets.

@alichry
Last active October 15, 2021 11:24
Show Gist options
  • Save alichry/feee07868cbccf80856ae291427e3bc2 to your computer and use it in GitHub Desktop.
Save alichry/feee07868cbccf80856ae291427e3bc2 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
script_name="$(basename "${0}")"
usage="${script_name}: force retries executing a custom command until it exits successfully.
Usage:
${script_name} COMMAND ARG1 ARG2 ...
Example:
${script_name} curl -C - --retry 5 -L -O {URL}"
if [ "$#" -lt 1 ]; then
echo "${usage}" 1>&2
exit 1
fi
count=1
execute() {
local ret
echo "Executing command: " "$@" 1>&2
if "$@"; ret="$?"; [ "$ret" -ne 0 ]; then
echo "Command failed with exit code $ret"
return "$ret"
fi
return 0
}
#while echo "Executing command:" "$@" 1>&2 && ! "$@"
while ! execute "$@"
do
sleep 1
echo "------------------------\n"
echo "Retrying... (attempt: ${count})"
count=$((count + 1))
done
@alichry
Copy link
Author

alichry commented Oct 15, 2021

Example:

$ retry.sh brew install adobe-acrobat-reader
Executing command:  brew install adobe-acrobat-reader
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 1 formula.

==> Downloading https://ardownload2.adobe.com/pub/adobe/reader/mac/AcrobatDC/2100720099/AcroRdrDC_2100720099_MUI.dmg
###############################################################           88.6%
curl: (18) transfer closed with 34607262 bytes remaining to read
Error: Download failed on Cask 'adobe-acrobat-reader' with message: Download failed: https://ardownload2.adobe.com/pub/adobe/reader/mac/AcrobatDC/2100720099/AcroRdrDC_2100720099_MUI.dmg
Command failed with exit code 1
------------------------

Retrying... (attempt: 1)
Executing command:  brew install adobe-acrobat-reader
==> Downloading https://ardownload2.adobe.com/pub/adobe/reader/mac/AcrobatDC/2100720099/AcroRdrDC_2100720099_MUI.dmg
################################################################          89.1%

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