Blog
Bash script to retry failed command
Nov 11, 2008
Category:Linux Bash
This was originally developed to retry an rsync command if a network error happened. It's a mishmash of code found from around the net, and could probably be tidier but it gets the job done.
function retry {
nTrys=0
maxTrys=5
status=256
until [ $status == 0 ] ; do
$1
status=$?
nTrys=$(($nTrys + 1))
if [ $nTrys -gt $maxTrys ] ; then
echo "Number of re-trys exceeded. Exit code: $status"
exit $status
fi
if [ $status != 0 ] ; then
echo "Failed (exit code $status)... retry $nTrys"
sleep 300
fi
done
}
usage:
retry "echo success"
retry "cat /failed/"