Answers:
是。
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上),无论使用--connect-timeout
和指定了什么时间,它都会在2分钟后停止尝试连接,尽管根据lib / connect.h中的宏,默认的连接超时值似乎为5分钟。DEFAULT_CONNECT_TIMEOUT
--max-time
似乎不存在默认值,curl
如果初始连接成功,将永远等待响应。
您可能对后一种选择感兴趣--max-time
。对于您的情况,将其设置为900
(15分钟)。
将选项指定--connect-timeout
为60
(一分钟)也可能是一个好主意。否则,curl
将尝试一次又一次地连接,显然使用一些退避算法。
有时间限制:/ 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.
比--max-time
是--speed-limit
和--speed-time
选项。简而言之,--speed-limit
指定您愿意接受的最低平均速度,并--speed-time
指定在传输超时和中止之前传输速度可以保持低于该限制的时间。
如果您在MacOS上安装了coreutils,则可以使用该软件包随附的GNU超时命令。GNU工具都以前缀,g
因此CLI为gtimeout
。
gtimeout --help
Usage: gtimeout [OPTION] DURATION COMMAND [ARG]...
or: gtimeout [OPTION]
Start COMMAND, and kill it if still running after DURATION.
$ gtimeout 1s curl -I http://www.google.com/
HTTP/1.1 200 OK
Date: Wed, 31 Oct 2018 03:36:08 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2018-10-31-03; expires=Fri, 30-Nov-2018 03:36:08 GMT; path=/; domain=.google.com
HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
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