4 ms·
I posted this because it worked perfectly for what I needed it for: a watchdog script on a cron job that issuis a PING to a process that occasionally times out.
by geocrasher 4y ago
I posted this because it worked perfectly for what I needed it for: a watchdog script on a cron job that issuis a PING to a process that occasionally times out. I'm sure it has a million flaws. Everything in Bash does, depending on who you ask. I'm certain there are a thousand reasons it won't work for somebody's application. It worked for mine.
- pkkm 4y agoChanged a bit in case of wrong or weird input. (The quotes on the right side of assignments aren't necessary but I find it to be a good practice to use quotes everywhere.) #!/usr/bin/env bash function retry { local retries="$1" shift case "$retries" in ""|*[!0-9]*) echo "retry: First argument must be a number, got '$retries'" 1>&2 return 1 ;; esac local count=0 until "$@"; do local exit="$?" local wait="$((2 ** count))" count="$((count + 1))" if [ "$count" -lt "$retries" ]; then echo "Retry $count/$retries exited $exit, retrying in $wait seconds..." 1>&2 sleep "$wait" else echo "Retry $count/$retries exited $exit, no more retries left." 1>&2 return "$exit" fi done return 0 } retry "$@"