linux shell curl 超时与重试
目录
curl 关于时间控制和重试的参数
连接超时参数 connect-timeout
请求超时时间 --max-time
请求重试 retry
重试超时时间 retry-max-time
重试延迟 retry-delay
curl 的功能非常强大, 参数也很繁多, 我们不仅常用于命令行, 在php中也有类似 curl 拓展的实现, 并且也对 libcurl 库提供了非常好的支持.
curl 项目: github.com/curl/curl
curl 关于时间控制和重试的参数
curl --help--connect-timeout SECONDS Maximum time allowed for connection
-m, --max-time SECONDS Maximum time allowed for the transfer...--retry NUM Retry request NUM times if transient problems occur
--retry-delay SECONDS Wait SECONDS between retries
--retry-max-time SECONDS Retry only within this period
上面是我整理的一部分跟时间有关系的参数, 虾米啊我们依次实验下.
连接超时参数 connect-timeout
说明
--connect-timeout SECONDS Maximum time allowed for connection
示例
#这里我们设置超时时间为2s, 请求一个无法解析的地址
curl --connect-timeout 2 --url http://xxx.comcurl: (28) Connection timed out after 2002 milliseconds
显示连接超时, 超时时间2002毫秒. 注意这个 warning 的时间可能每次统计不太一样, 一般会超过我们的预设值一点.
#对于一个对返回时间要求比较高的情况, 可以设置为浮点型精确到毫秒
curl --connect-timeout 0.3 --url http://xxx.comcurl: (28) Connection timed out after 300 milliseconds
请求超时时间 --max-time
说明
-m, --max-time SECONDS Maximum time allowed for the transfer
示例
#这里我们设置超时时间为2s, 应用程序中sleep 2
curl --max-time 2 --url http://www.shuai.comcurl: (28) Operation timed out after 2002 milliseconds with 0 bytes received
connect-time 和 max-time 联合使用:
#这里我们使用了一个无法解析的地址
curl --connect-time 3 --max-time 2 --url http://xxx.com
> curl: (28) Connection timed out after 2001 millisecondscurl --connect-time 3 --max-time 4 --url http://xxx.com
> curl: (28) Operation timed out after 4002 milliseconds with 0 bytes received
这里我们发现返回结果为连接超时 2001 毫秒, 当共同使用时, 连接以最小时间的为准, 而返回时间已 max-time 限制为准.
请求重试 retry
说明
--retry NUM Retry request NUM times if transient problems occur
示例
#同样,我们去请求一个 sleep 2 的地址
curl --max-time 0.1 --retry 3 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
> Warning: Transient problem: timeout Will retry in 4 seconds. 1 retries left.
> curl: (28) Operation timed out after 100 milliseconds with 0 bytes received
我们发现重试了3次, 但它并不是失败后立刻重试, 而是第一次 1 s后重试, 第二次 2 s后重试, 第三次 4 s后重试,依次递增 (每次重试受 max-time 限制).
重试超时时间 retry-max-time
我们发现我们的 max-time 只是对单次请求做了时间限制, 进而去影响总的重试时间, 但是我们想在单位时间内完成重试该怎么做呢. 这里 curl 也提供了重试的超时时间 retry-max-time
curl --retry 3 --retry-max-time 2 --max-time 0.1 --url http://www.shuai.com> Warning: Transient problem: timeout Will retry in 1 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 2 seconds. 2 retries left.
> curl: (28) Operation timed out after 101 milliseconds with 0 bytes received
我们对重试总的超时时间设置为2s, 配置了3次重试, 但仅仅完成了两次重试就超时结束了.
重试延迟 retry-delay
我们在 请求重试 里面讲到, 这里的重试并不是失败后立刻重试的, 默认重试时间递增, 这里我们可以使用 retry-delay 控制重试的间隔.
#这里我们设置重试时间5s,重试3次
curl --retry 3 --retry-delay 5 --max-time 0.1 --url http://xxx.com> Warning: Transient problem: timeout Will retry in 5 seconds. 3 retries left.
> Warning: Transient problem: timeout Will retry in 5 seconds. 2 retries left.
> Warning: Transient problem: timeout Will retry in 5 seconds. 1 retries left.
> curl: (28) Connection timed out after 101 milliseconds
我们发现 Will retry in 变成了 5 s一次
linux shell curl 超时与重试相关推荐
- linux shell curl get 请求头 和多参数问题及解决
1.请求头Authorization 在curl 中添加请求头信息,需要用-H ,并且用"请求头属性:属性值"的格式,如 curl -H "Authorization:B ...
- curl批量调用linux,shell的简单批量curl接口脚本
shell脚本可以说作用非常大,在服务器领域,用shell操作事务可比手动点击要方便快捷得多了.虽然只是文字界面,但是其强大的处理功能,会让各种操作超乎想象.而且,也可以将这些习惯移植到日常的工作当中 ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
- Linux shell速查手册
Linux Shell脚本攻略笔记[速查] Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgr ...
- LINUX SHELL脚本攻略笔记[速查]
LINUX SHELL脚本攻略笔记[速查] Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgr ...
- 批量ping脚本shell_30个Linux Shell脚本经典案例(上)
在学习Linux运维时,普遍反馈是:Linux Shell是一个很难的知识板块.虽然大家都认真学,基本的语法也都掌握了,但有需求时,很难直接上手编程,要么写了很久,要么写不好! 也有很多做运维很多年的 ...
- 在linux上面找一个脚本,30个Linux Shell脚本经典案例
在学习Linux运维时,普遍反馈是:Linux Shell是一个很难的知识板块.虽然大家都认真学,基本的语法也都掌握了,但有需求时,很难直接上手编程,要么写了很久,要么写不好! 也有很多做运维很多年的 ...
- linux怎么判断网络是否正常运行,Linux shell脚本判断服务器网络是否可以上网
微信公众号:「小林coding」 用简洁的方式,分享编程小知识. Linux shell脚本判断网络畅通 介绍 在编写shell脚本时,有的功能需要确保服务器网络是可以上网才可以往下执行,那么此时就需 ...
- linux shell之文件锁
2019独角兽企业重金招聘Python工程师标准>>> 经常在 shell 脚本里要阻止其它进程,比如 msmtp 自带的mail queue 脚本,这个脚本的互斥做法是不正确的,下 ...
最新文章
- 传Exchange 15将于今年9月发布
- linux安装setup.py程序
- 眼睛很疼(2005-5-10)
- tensorflow随笔-非线性回归
- C++中两个数交换不引进中间变量的方法
- 《SAS编程与数据挖掘商业案例》学习笔记之十一
- beeshell —— 开源的 React Native 组件库
- HDU-4675 GCD of Sequence 数学
- helm使用_通过图表提升您在Kubernetes上使用Helm的能力
- Python菜鸟入门:day03运算符
- SAP ABAP里数据库表的Storage Parameters从哪里来的 1
- Db4o数据库:细说查询
- Navicat Premium 连接 MongoDB 失败: Cannot connect to MongoDB.No suitable servers found: ......
- eeepc linux 软件管理,Linux_为EeePC增添活力 EeePC安装Ubuntu预览,虽然Eee PC运行基于Xandros的Linux - phpStudy...
- windows 连接蓝牙耳机失败 解决方法
- 支付宝支付学习:蚂蚁沙箱环境是什么?怎样使用蚂蚁沙箱环境?
- VS2010 Ultimate 微软官网免费下载 VS2010终级版
- uniapp unipush接入个推消息推送(各种踩坑记录)安卓+IOS+在线消息+离线消息
- 审稿审什么你真的弄清楚了吗
- GSM 03.38 from Wikipedia
热门文章
- 架构师之路 — 分布式系统 — RPC 远程过程调用
- DPDK — RTE_LOG 日志模块
- MAC Pro 同时安装 Python2 和 Python3
- Linux_SquidProxyServer代理服务器
- Altium Designer中如何仅显示顶层的元件和顶层布线
- 搜索引擎(Elasticsearch聚合分析)
- ThinkCMF 5.1.0 发布:支持 swoole、协议变更为 MIT
- 当我们在讨论奢侈品行业时,人工智能可以做什么?
- Swt/Jface中提供的dialog
- TurnipBit:可以带着孩子一起玩编程的MicroPython开发板!