Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl returns empty reply from server bash due to curl failure

Tags:

bash

curl

get

i am writing a simple bash script to "curl get" some values. Sometimes the code works and sometimes it fails, and says "empty reply from server". How to set up a check for this in bash so that if the curl fails once it tries again until it gets the values?

like image 511
david419 Avatar asked Mar 21 '26 22:03

david419


2 Answers

while ! curl ...    # add your specific curl statement here
do
    { echo "Exit status of curl: $?"
      echo "Retrying ..."
    } 1>&2
    # you may add a "sleep 10" or similar here to retry only after ten seconds
done

In case you want the output of that curl in a variable, feel free to capture it:

output=$(
  while ! curl ...    # add your specific curl statement here
  do
      { echo "Exit status of curl: $?"
        echo "Retrying ..."
      } 1>&2
      # you may add a "sleep 10" or similar here to retry only after ten seconds
  done
)

The messages about the retry are printed to stderr, so they won't mess up the curl output.

like image 76
Alfe Avatar answered Mar 23 '26 23:03

Alfe


People are overcomplicating this:

until contents=$(curl "$url")
do
  sleep 10
done
like image 30
that other guy Avatar answered Mar 24 '26 01:03

that other guy