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?
Asked
Active
Viewed 3,715 times
4 Answers
4
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.

Alfe
- 56,346
- 20
- 107
- 159
3
People are overcomplicating this:
until contents=$(curl "$url")
do
sleep 10
done

that other guy
- 116,971
- 11
- 170
- 194
1
For me sometimes it happens when curl timed out and there is no information about that. Try curl with --connect-timeout 600 (in seconds) like:
curl --connect-timeout 600 "https://api.morph.io/some_stuff/data.json"
Maybe this helps you.

Tomas Pytel
- 188
- 2
- 14
0
if you wanted to try the command until it succeeded, you could say:
command_to_execute; until (( $? == 0 )); do command_to_execute; done

chepner
- 497,756
- 71
- 530
- 681

Jayesh Bhoi
- 24,694
- 15
- 58
- 73