Linux

檢查 url/文件是否存在並下載的方法

  • June 9, 2020

bash 中是否有任何適當的方法來檢查是否存在 - zip 文件並下載它。如果沒有文件,請等待它並在可用時下載。因此,例如,它每 x 秒檢查一次 url,如果可用則下載文件並退出,否則等待它下載。就像是:

if curl --head --silent --fail -X POST https://192.168.1.2/file/file.zip 2> /dev/null;
then
   wget https://192.168.1.2/file/file.zip
else
   sleep 60 && #check every x seconds and download when available"
fi

謝謝,

您可以簡單地重新執行該命令,直到它成功:

while ! wget https://192.168.1.2/file/file.zip; do
   sleep 60
done

wget具有用於檢查和重試的本機選項,您可能會發現其中一些有用並相應地組合它們:

--spider
          When invoked with this option, Wget will behave as a Web spider, which means that it will not download the pages, just check that they are there.

-w seconds
      --wait=seconds
          Wait the specified number of seconds between the retrievals.  Use of this option is recommended, as it lightens the server load by making the requests less frequent.  Instead
          of in seconds, the time can be specified in minutes using the "m" suffix, in hours using "h" suffix, or in days using "d" suffix.

          Specifying a large value for this option is useful if the network or the destination host is down, so that Wget can wait long enough to reasonably expect the network error to
          be fixed before the retry.  The waiting interval specified by this function is influenced by "--random-wait", which see.
   enter code here

-t number
      --tries=number
          Set number of tries to number. Specify 0 or inf for infinite retrying.  The default is to retry 20 times, with the exception of fatal errors like "connection refused" or "not
          found" (404), which are not retried.

還有更多的man wget

引用自:https://unix.stackexchange.com/questions/591793