检查 url/文件是否存在并下载的方法

检查 url/文件是否存在并下载的方法

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

谢谢你,

答案1

您可以简单地重新运行命令直到成功:

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

答案2

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

相关内容