假设我是一个 Unix 大菜鸟:
我每 15 分钟就通过 cron 运行一次curl 请求。
Curl 基本上用于加载给定一些参数的网页(PHP),充当脚本,例如:
curl http://example.com/?update_=1
我想要实现的是使用这种卷曲技术运行另一个“脚本”,
我读到,curl 在一个命令中接受多个 URL,但我不确定这是否会按顺序或“并行”处理 URL。
它很可能会按顺序处理它们(为什么不直接测试它)。但你也可以这样做:
创建一个名为
curlrequests.sh
的文件
将其放入文件中,如下所示:
curl http://example.com/?update_=1
curl http://example.com/?update_=3
curl http://example.com/?update_=234
curl http://example.com/?update_=65
保存文件并使其可执行
chmod
:
chmod +x curlrequests.sh
运行您的文件:
./curlrequests.sh
或
/path/to/file/curlrequests.sh
顺便说一句,您可以使用
&&
链接请求,如下所示:
curl http://example.com/?update_=1 && curl http://example.com/?update_=2 && curl http://example.com?update_=3`
并使用
&
: 并行执行
curl http://example.com/?update_=1 & curl http://example.com/?update_=2 & curl http://example.com/?update_=3
根据 curl 手册页:
您可以在命令行上指定任意数量的 URL。他们会 按指定顺序依次获取。
因此,最简单、最有效的(curl 会将它们全部发送到单个 TCP 连接 [那些发送到同一源的])方法是将它们全部放在一次 curl 调用上,例如:
curl http://example.com/?update_=1 http://example.com/?update_=2
此处未提及的另一种关键方法是使用 用于多个 HTTP 请求的同一个 TCP 连接,并且为此使用一个curl 命令。
这对于节省网络带宽、客户端和服务器资源以及总体使用多个curl命令的需要非常有用,因为默认情况下curl会在到达命令结束时关闭连接。
保持连接打开并重用它对于运行 Web 应用程序的标准客户端来说非常常见。
从 curl 版本 7.36.0 开始,
--next
或 -:
命令行选项允许链接多个请求,并且可在命令行和脚本中使用。
例如:
curl http://example.com/?update_=1 -: http://example.com/foo
curl http://example.com/?update_=1 -: -d "I am posting this string" http://example.com/?update_=2
curl -o 'my_output_file' http://example.com/?update_=1 -: -d "my_data" -s -m 10 http://example.com/foo -: -o /dev/null http://example.com/random
来自 curl 手册页:
-:, --下一个
告诉curl对以下URL使用单独的操作 相关选项。这允许您发送多个 URL 请求,每个请求 具有自己的特定选项,例如不同的用户 每个的名称或自定义请求。
、-:
将重置所有本地选项,只有全局选项的值才会保留到 -:, --next 之后的操作中 操作说明。全局选项包括 -v、--verbose、--trace、 --trace-ascii 和 --fail-early。--next
例如,您可以在单个命令中同时执行 GET 和 POST 线路:
curl www1.example.com --next -d postthis www2.example.com
7.36.0 中添加。
我发现您的网址末尾有一个数字。我最近遇到了同样的问题,该数字是从 0 到 13 的连续数字。以下是我如何用一行解决该问题的方法:
curl http://example.com/?update_=[0-13]
例如,如果您只想要偶数,您可以指定跳转:
curl http://example.com/?update_=[0-13:2]
这里是curl 手册页的逐字记录:
URL 语法与协议相关。您可以在 RFC 3986 中找到详细描述。
您可以通过在大括号内编写部分集并引用 URL 来指定多个 URL 或 URL 的一部分,如下所示:
"http://site.{one,two,three}.com"
或者您可以使用 [] 获取字母数字系列的序列,如下所示:
"ftp://ftp.example.com/file[1-100].txt"
(带前导零)"ftp://ftp.example.com/file[001-100].txt"
"ftp://ftp.example.com/file[a-z].txt"
不支持嵌套序列,但您可以相邻使用多个序列:
"http://example.com/archive[1996-1999]/vol[1-4]/part{a,b,c}.html"
我认为这使用了更多本机功能
//printing the links to a file
$ echo "https://stackoverflow.com/questions/3110444/
https://stackoverflow.com/questions/8445445/
https://stackoverflow.com/questions/4875446/" > links_file.txt
$ xargs curl < links_file.txt
按照所需的顺序编写一个包含两个curl请求的脚本,并通过cron运行它,就像
#!/bin/bash
curl http://mysite.com/?update_=1
curl http://mysite.com/?the_other_thing
这将做你想做的事,使用输入文件并且速度超级快
#!/bin/bash
IFS=$'\n'
file=/path/to/input.txt
lines=$(cat ${file})
for line in ${lines}; do
curl "${line}"
done
IFS=""
exit ${?}
输入文件中每行一个条目,它将遵循输入文件的顺序
将其另存为whatever.sh并使其可执行
您还可以使用括号{}
假设您想要卷曲:
a. wildfly_datasources_jdbc_total
b. wildfly_datasources_jdbc_currently
curl http://127.0.0.1:9990/metrics/vendor/wildfly_datasources_jdbc_{total,currently}