由于curl失败,curl从服务器bash返回空回复

由于curl失败,curl从服务器bash返回空回复,bash,curl,get,Bash,Curl,Get,我正在编写一个简单的bash脚本来“curl-get”一些值。有时代码工作,有时失败,并显示“来自服务器的空回复”。 如何在bash中为此设置一个检查,以便在curl失败一次时,它再次尝试,直到获得值为止 while ! curl ... # add your specific curl statement here do { echo "Exit status of curl: $?" echo "Retrying ..." } 1>&2

我正在编写一个简单的bash脚本来“curl-get”一些值。有时代码工作,有时失败,并显示“来自服务器的空回复”。 如何在bash中为此设置一个检查,以便在curl失败一次时,它再次尝试,直到获得值为止

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
如果希望将该旋度的输出放在变量中,请随意捕获它:

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
)

有关重试的消息将打印到stderr,这样它们就不会扰乱curl输出。

如果您想尝试该命令直到成功,可以说:

command_to_execute; until (( $? == 0 )); do command_to_execute; done

人们正在将这一点过度复杂化:

until contents=$(curl "$url")
do
  sleep 10
done

对我来说,有时候它发生在curl超时的时候,而且没有关于它的信息。尝试使用--connect timeout 600(以秒为单位)进行curl,如下所示:


这可能会对您有所帮助。

在执行curl check exit code variable
$?
之后,如果需要,请重试(不是零)。添加了关于捕获输出的部分。
  curl --connect-timeout 600 "https://api.morph.io/some_stuff/data.json"