到目前为止,我确实找不到任何东西,但是真的curl
根本不会超时吗?
user@host:~# curl http://localhost/testdir/image.jpg
我这么问是因为我将对图像的任何请求重定向testdir
到一个单独的 Apache 模块,该模块会动态生成这些图片。图片实际准备好并交付给发出请求的客户可能需要长达 15 分钟的时间。
总是curl
等待(或者取决于配置)或者是否有任何超时?
答案1
是的。
超时参数
curl
有两个选项:--connect-timeout
和--max-time
。
引用手册页:
--connect-timeout <seconds>
Maximum time in seconds that you allow the connection to the
server to take. This only limits the connection phase, once
curl has connected this option is of no more use. Since 7.32.0,
this option accepts decimal values, but the actual timeout will
decrease in accuracy as the specified timeout increases in deci‐
mal precision. See also the -m, --max-time option.
If this option is used several times, the last one will be used.
和:
-m, --max-time <seconds>
Maximum time in seconds that you allow the whole operation to
take. This is useful for preventing your batch jobs from hang‐
ing for hours due to slow networks or links going down. Since
7.32.0, this option accepts decimal values, but the actual time‐
out will decrease in accuracy as the specified timeout increases
in decimal precision. See also the --connect-timeout option.
If this option is used several times, the last one will be used.
默认值
这里(在 Debian 上)它会在 2 分钟后停止尝试连接,无论使用 和 指定的时间如何--connect-timeout
,尽管默认的连接超时值似乎是5分钟根据DEFAULT_CONNECT_TIMEOUT
宏中库/连接.h。
的默认值--max-time
似乎不存在,curl
如果初始连接成功,则将永远等待响应。
用什么?
您可能对后一个选项感兴趣--max-time
。根据您的情况,将其设置为900
(15 分钟)。
将选项指定--connect-timeout
为60
(一分钟)之类的内容也可能是一个好主意。否则curl
将尝试一次又一次地连接,显然使用了某种退避算法。
答案2
有时间限制:/usr/bin/timelimit - 有效限制进程的绝对执行时间
Options:
-p If the child process is terminated by a signal, timelimit
propagates this condition, i.e. sends the same signal to itself.
This allows the program executing timelimit to determine
whether the child process was terminated by a signal or
actually exited with an exit code larger than 128.
-q Quiet operation - timelimit does not output diagnostic
messages about signals sent to the child process.
-S killsig
Specify the number of the signal to be sent to the
process killtime seconds after warntime has expired.
Defaults to 9 (SIGKILL).
-s warnsig
Specify the number of the signal to be sent to the
process warntime seconds after it has been started.
Defaults to 15 (SIGTERM).
-T killtime
Specify the maximum execution time of the process before
sending killsig after warnsig has been sent. Defaults to 120 seconds.
-t warntime
Specify the maximum execution time of the process in
seconds before sending warnsig. Defaults to 3600 seconds.
On systems that support the setitimer(2) system call, the
warntime and killtime values may be specified in fractional
seconds with microsecond precision.
答案3
--max-time
比--speed-limit
和选项更好--speed-time
。简而言之,--speed-limit
指定您愿意接受的最小平均速度,并--speed-time
指定在传输超时并中止之前传输速度可以保持低于该限制的时间。
答案4
BASH4+ 中的几个解决方案
# -- server available to check via port xxx ? --
function isServerAvailableNC() {
max_secs_run="${3}"
if timeout $max_secs_run nc -z ${1} ${2} 2>/dev/null >/dev/null; then
#echo "${1} ✓"
true
else
#echo "${1} ✗"
return
fi
}
# -- server available to check via port xxx ? --
# -- supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE) --
#/usr/bin/curl -sSf --max-time 3 https://ifwewanted.to.confirm.https.com/ --insecure
function isServerAvailableCURL() {
max_secs_run="${3}"
proto="http://"
if [ ! -z ${2} ] || [ ${2} -gt 80 ] ;then
proto="https://"
fi
if /usr/bin/curl -sSf --max-time "${max_secs_run}" "${1}" --insecure 2>/dev/null >/dev/null; then
#echo "${1} ✓"
true
else
#echo "${1} ✗"
false
fi
}
使用示例:
如果我们需要特定端口,建议使用 NC
host="1.2.3.4"
if isServerAvailableCURL "$host" "80" "3";then
check_remote_domain_cert "$host"
fi
host="1.2.3.4"
if isServerAvailableNC "$host" "80" "3";then
check_remote_domain_cert "$host"
fi