Last night, I ran $CMD $ARG manually a few times, because the server kept returning errors causing the command exited with non-zero exit status. It was late, and I wouldn’t want to stay late just to keep doing that, so I issued the following simple one-liner shell script:

while ! $CMD $ARG; do sleep 1m; done

It really was a simple loop, it negated the exit status using !, so it would keep running the loop every time $CMD fails with an interval of one minute until the command finally exits successfully causing the loop to end, therefore the script finishes.

This morning after the command finally executed normally, I was wondering if there is a retry command. I had written the similar loops a few times whenever I needed to rerun a command because of failures.

After a few searches, I couldn’t find one. However, this is one close to what I have in mind, without writing a shell script, although it still uses !:

watch -n $INTERVAL -e ! timeout $TIMEOUT $CMD $ARG

With watch, $CMD can be executed with $INTERVAL if it fails, there is also a timeout to make sure the command doesn’t run too longer. ! will make successful execution of the command become an error, for which, -e options would take action to stop watching, and that is the result we want.

Normally, for retrying, there should be a number of retires option for maximal retires, but not with these commands.

I would believe somewhere out there, someone has done it in one simple command that you can run something like:

$RETRY -r $RETRIES -i $INTERVAL -t $TIMEOUT $CMD $ARG

If you know of such command or a better way to deal with this kind of task, please let me know.