This content originally appeared on DEV Community and was authored by rinaxsumomo
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=0
RESULT=0
until [ ${NEXT_WAIT_TIME} -eq 4 ] || YOUR_COMMAND
do
sleep $(( NEXT_WAIT_TIME++ ))
done
if [[ ${NEXT_WAIT_TIME} -eq 4 ]]; then
RESULT=1
fi
if [[ ${RESULT} -eq 0 ]]; then
echo "Command Successful"
else
echo "Command Failed"
fi
This content originally appeared on DEV Community and was authored by rinaxsumomo
rinaxsumomo | Sciencx (2021-09-28T07:23:22+00:00) How to retry and log the final result in Bash. Retrieved from https://www.scien.cx/2021/09/28/how-to-retry-and-log-the-final-result-in-bash/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.