Created
February 16, 2023 13:14
-
-
Save AvnerCohen/472fa2c0c31bd7fd71f77f608e54eeb4 to your computer and use it in GitHub Desktop.
Retry bash and sleep between retries, also limit number retries
This file contains 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 | |
RETRIES=5 | |
SLEEP_IN_SECONDS=2 | |
RETURN_CODE=99 | |
counter=0 | |
until [[ $RETURN_CODE -eq "0" || $counter -ge $RETRIES ]] ; do | |
# next line you should run the command, false and echo are just sample placeholders for the test | |
echo "Command to execute" && false | |
RETURN_CODE=$? | |
sleep $SLEEP_IN_SECONDS | |
echo "Attempt [$counter/$RETRIES] (Latest return code $RETURN_CODE)" | |
counter=$((counter+1)) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment