Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop a curl call & count successful responses before an error

I'm trying to test and quantify a intermittent error response from an http server. I have a curl call constructed that can make the necessary request, but I'm getting tired up typing up arrow and return.

How can I tell curl to repeatedly make the request until it encounters an error and then output the number of success responses before the error?

like image 967
Matt V. Avatar asked Feb 07 '23 14:02

Matt V.


1 Answers

This bash recipe should do what you're looking for.

i=0 ; while true ; do curl -o /dev/null -s https://www.google.com/ ; if [ $? -ne 0 ] ; then echo $i ; break ; fi ; i=$(($i+1)) ; echo -en "$i        \r" ; sleep 1 ; done

Broken out into multiple lines:

i=0 # set counter to 0
while true  # infinite loop
do
    curl -o /dev/null -s https://www.google.com/  # silent curl request to site
    if [ $? -ne 0 ]
    then
        # curl didn't return 0 - failure
        echo $i
        break # terminate loop
    fi
    i=$(($i+1))  # increment counter
    echo -en "$i        \r"   # display # of requests each iteration
    sleep 1  # short pause between requests
done

You can optionally redirect cURL output to a file, so when it fails you can see the output using something like this:

 curl -v -o /dev/null https://google.com &> /tmp/output.txt
like image 189
drew010 Avatar answered Feb 19 '23 17:02

drew010