An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
September 28, 2021 07:23 am GMT
Original Link: https://dev.to/rinaxsumomo/how-to-retry-and-log-the-final-result-in-bash-17f0
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To