Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Created February 16, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/472fa2c0c31bd7fd71f77f608e54eeb4 to your computer and use it in GitHub Desktop.
Save AvnerCohen/472fa2c0c31bd7fd71f77f608e54eeb4 to your computer and use it in GitHub Desktop.
Retry bash and sleep between retries, also limit number retries
#!/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