由于卷曲失败,curl返回来自服务器bash的空回复

时间:2014-03-07 12:22:05

标签: bash curl get

我正在编写一个简单的bash脚本来“卷曲”一些值。有时代码有效,有时会失败,并说“来自服务器的空回复”。 如何在bash中设置对此进行检查,以便在卷曲失败后再次尝试直到获得值为止?

4 个答案:

答案 0 :(得分:3)

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,因此它们不会弄乱卷曲输出。

答案 1 :(得分:2)

人们过于复杂:

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

答案 2 :(得分:1)

对于我来说,有时会发生卷曲超时并且没有相关信息。尝试curl with --connect-timeout 600(以秒为单位),如:

  curl --connect-timeout 600 "https://api.morph.io/some_stuff/data.json"

也许这有助于你。

答案 3 :(得分:0)

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

command_to_execute; until (( $? == 0 )); do command_to_execute; done
相关问题