Last active
July 8, 2024 12:34
-
-
Save dublx/e99ea94858c07d2ca6de to your computer and use it in GitHub Desktop.
Bash - retry command N times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 | |
You're welcome!
Thanks ...
What is the purpose of set -euo pipefail ?
this solved my problem too but how do we modify the retry logic to check for specific strings in the command output and then retry instead of just checking the exit code of the command.
Sorry @BD8806 I didnt see this last question - did you sort it out in the end?
for i in {1..5}; do myFunction && break || sleep 15; done
for i in {1..5}; do myFunction && break || sleep 15; done
Perfect, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you ! that's exactly what I need. I wasn't sure how to do it without removing set -e