Instantly share code, notes, and snippets.

@dublx /retry.sh
Last active Jul 11, 2018

Embed
What would you like to do?
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
@dublx

This comment has been minimized.

Owner

dublx commented Jan 5, 2015

Use this code to retry a Bash command/function N times with a retry interval.

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