我通过 wget
在 Linux 上设置了一些 cron 作业,这些作业每 24 小时运行一次。所有的工作基本上都是调用 API,提取数据,我在数据库上很强大。现在的问题是一些 API 调用非常非常慢,需要花费大量时间才能获得响应,最终导致出现以下错误。
--2017-07-24 06:00:02-- http://wwwin-cam-stage.cisco.com/cron/mh.php Resolving wwwin-cam-stage.cisco.com (wwwin-cam-stage.cisco.com)... 171.70.100.25 Connecting to wwwin-cam-stage.cisco.com (wwwin-cam-stage.cisco.com)|171.70.100.25|:80... connected. HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers. Retrying.
--2017-07-24 06:05:03-- (try: 2) http://wwwin-cam-stage.cisco.com/cron/mh.php Connecting to wwwin-cam-stage.cisco.com (wwwin-cam-stage.cisco.com)|171.70.100.25|:80... connected. HTTP request sent, awaiting response... Read error (Connection reset by peer) in headers. Retrying.
--2017-07-24 06:10:05-- (try: 3) http://wwwin-cam-stage.cisco.com/cron/mh.php Connecting to wwwin-cam-stage.cisco.com (wwwin-cam-stage.cisco.com)|171.70.100.25|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 [text/html] Saving to: ‘mh.php.6’
0K 0.00 =0s
2017-07-24 06:14:58 (0.00 B/s) - ‘mh.php.6’ saved [0/0]
虽然在第三次尝试时它给出了响应 200 OK,但它在第一次和第二次尝试超时时弄乱了实际数据。
如何将 URL 超时设置更改为无限时间或最高限制,以便一次完成工作并且不会出现类似
的错误(Connection reset by peer)....
最佳答案
wget --timeout 10 http://url
这可以用在 wget
的情况下。
编辑
或者
如果您询问的是 Linux 机器的 Keep-Alive,这可能会有所帮助。
在 RedHat Linux 上,通过编辑/etc/sysctl.conf 文件修改以下内核参数,并重新启动网络守护进程 (/etc/rc.d/init.d/network restart
)。
"Connection reset by peer" is the TCP/IP equivalent of slamming the phone back on the hook. It's more polite than merely not replying, leaving one hanging. But it's not the FIN-ACK expected of the truly polite TCP/IP converseur.
代码:
# Decrease the time default value for tcp_keepalive_time
tcp_keepalive_time = 1800
编辑
-T秒
‘--timeout=seconds’
将网络超时设置为 seconds 秒。这等效于指定 '--dns-timeout'
、'--connect-timeout'
和 '--read-timeout'
, 同时进行。
当与网络交互时,Wget 可以检查超时并在超时时中止操作。这可以防止诸如挂起读取和无限连接之类的异常。默认情况下启用的唯一超时是 900 秒读取超时。将超时设置为 0 将完全禁用它。除非您知道自己在做什么,否则最好不要更改默认超时设置。
所有与超时相关的选项都接受十进制值以及亚秒值。例如,“0.1”秒是合法(尽管不明智)的超时选择。亚秒级超时对于检查服务器响应时间或测试网络延迟很有用。
‘--dns-timeout=seconds’
将 DNS 查找超时设置为 seconds 秒。未在指定时间内完成的 DNS 查找将失败。默认情况下,除了系统库实现的超时之外,DNS 查找没有超时。
‘--connect-timeout=seconds’
将连接超时设置为 seconds 秒。建立时间较长的 TCP 连接将被中止。默认情况下,没有连接超时,系统库实现的除外。
‘--read-timeout=seconds’
将读取(和写入)超时设置为 seconds 秒。这个超时的“时间”是指空闲时间:如果在下载的任何时刻,超过指定的秒数没有收到数据,则读取失败并重新开始下载。此选项不会直接影响整个下载的持续时间。
当然,远程服务器可以选择比该选项要求的更早终止连接。默认读取超时为 900 秒。
来源 Wget 手册。
查看此 wget Manual page获取更多信息。
关于php - 如何更改 Linux 网络服务器上的 URL 超时设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45286303/