Skip to content

Instantly share code, notes, and snippets.

@markoradinovic
Forked from dublx/retry.sh
Created July 3, 2017 22:20
Show Gist options
  • Save markoradinovic/8bf6e0711d2b2c7d232dd044e62c053a to your computer and use it in GitHub Desktop.
Save markoradinovic/8bf6e0711d2b2c7d232dd044e62c053a to your computer and use it in GitHub Desktop.
Bash - retry command N times
#!/bin/bash
set -euo pipefail
function myFunction() {
myCommand
return $?
}
retry=0
maxRetries=5
retryInterval=15
until [ ${retry} -ge ${maxRetries} ]
do
myFunction && break
retry=$[${retry}+1]
echo "Retrying [${retry}/${maxRetries}] in ${retryInterval}(s) "
sleep ${retryInterval}
done
if [ ${retry} -ge ${maxRetries} ]; then
echo "Failed after ${maxRetries} attempts!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment