Skip to content

Instantly share code, notes, and snippets.

@sherwind
Created April 9, 2018 11:08
Show Gist options
  • Save sherwind/c9c69976e1337976b631410343b9118e to your computer and use it in GitHub Desktop.
Save sherwind/c9c69976e1337976b631410343b9118e to your computer and use it in GitHub Desktop.
bash function to retry command
retry_cmd()
{
attempts=15
false
while [ "${?}" -gt 0 ]; do
if [ "${attempts}" -eq 0 ]; then
logger --tag "$0" -- "Failed to run $*: $output"
return
fi
logger --tag "$0" -- "Trying to run $*"
output=$("$@")
if [ "${?}" -gt 0 ]; then
let attempts--
sleep 2
false
fi
done
echo "${output}"
}
retry_cmd /path/to/command --arg1 arg1 --arg2 arg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment