Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 28, 2021 07:23 am GMT

How to retry and log the final result in Bash

I know there are lots of how-to write retry logic in Bash, such as here.

I've decided to use the one in the accepted answer above.
However I wanted to add another logic to log the final result as well, so I needed to modify this snippet as sleep always returns exit code 0 at last that logging depending on the exit code would not work as it always returns 0, even the command to be retried ended up with failure.

So here is my snippet. Below runs YOUR_COMMAND until success, 4 times for the maximum, and echo the result based on the exit code. This might not work depending on how YOUR_COMMAND returns a result, but it worked in my case.

NEXT_WAIT_TIME=0RESULT=0until [ ${NEXT_WAIT_TIME} -eq 4 ] || YOUR_COMMANDdo    sleep $(( NEXT_WAIT_TIME++ ))doneif [[ ${NEXT_WAIT_TIME} -eq 4 ]]; then    RESULT=1fiif [[ ${RESULT} -eq 0 ]]; then    echo "Command Successful"else    echo "Command Failed"fi

Original Link: https://dev.to/rinaxsumomo/how-to-retry-and-log-the-final-result-in-bash-17f0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To