Skip to content

Instantly share code, notes, and snippets.

@mhutter
Created June 26, 2018 15:12
Show Gist options
  • Save mhutter/9162b18805f850c2069fce074bc34d52 to your computer and use it in GitHub Desktop.
Save mhutter/9162b18805f850c2069fce074bc34d52 to your computer and use it in GitHub Desktop.
Bash: Retry command N times
#!/bin/bash
function retry {
local n=0
local max="$1"; shift
local cmd="$*"
until $cmd; do
if [ "$((++n))" -lt "$max" ]; then
echo "Retrying in $((n*2)) seconds..."
sleep "$((n*2))"
else
echo "Failed after $n attempts: $cmd"
return 1
fi
done
}
retry 3 ping foobar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment