如何运行按顺序处理的多个curl 请求?

发布于 2024-09-06 20:37:38 字数 405 浏览 6 评论 0原文

假设我是一个 Unix 新手:

  • 我正在通过 cron 运行一个curl请求每 15 分钟一班。

  • Curl 基本上用于加载给定一些参数的网页(PHP),充当如下脚本:

    curl http://example.com/?update_=1
    

我想要实现的是使用这种curl 技术运行另一个“脚本”,

  • 每次在另一个脚本运行
  • 之前运行其他脚本

我已经读到,curl 在一个命令中接受多个 URL,但我不确定这是否会按顺序或“并行”处理 URL。

Assuming I'm a big Unix rookie:

  • I'm running a curl request through cron every 15 minutes.

  • Curl basically is used to load a web page (PHP) that given some arguments, acts as a script like:

    curl http://example.com/?update_=1
    

What I would like to achieve is to run another "script" using this curl technique,

  • every time the other script is run
  • before the other script is run

I have read that curl accepts multiple URLs in one command, but I'm unsure if this would process the URLs sequentially or in "parallel".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吃颗糖壮壮胆 2024-09-13 20:37:38

它很可能会按顺序处理它们(为什么不直接测试它)。但您也可以这样做:

  1. 创建一个名为 curlrequests.sh

    的文件

  2. 将其放入如下文件中:

    curl http://example.com/?update_=1
    卷曲 http://example.com/?update_=3
    卷曲 http://example.com/?update_=234
    卷曲 http://example.com/?update_=65
    
  3. 保存文件并使用chmod使其可执行:

    chmod +xcurlrequests.sh
    
  4. 运行文件:

    <前><代码>./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

It would most likely process them sequentially (why not just test it). But you can also do this:

  1. make a file called curlrequests.sh

  2. put it in a file like thus:

    curl http://example.com/?update_=1
    curl http://example.com/?update_=3
    curl http://example.com/?update_=234
    curl http://example.com/?update_=65
    
  3. save the file and make it executable with chmod:

    chmod +x curlrequests.sh
    
  4. run your file:

    ./curlrequests.sh
    

or

   /path/to/file/curlrequests.sh

As a side note, you can chain requests with &&, like this:

   curl http://example.com/?update_=1 && curl http://example.com/?update_=2 && curl http://example.com?update_=3`

And execute in parallel using &:

   curl http://example.com/?update_=1 & curl http://example.com/?update_=2 & curl http://example.com/?update_=3
我也只是我 2024-09-13 20:37:38

根据 curl 手册页

您可以在命令行上指定任意数量的 URL。他们将是
按指定顺序按顺序获取。

因此,最简单和最有效的(curl 将把它们全部发送到单个 TCP 连接 [那些到同一来源])方法是将它们全部放在一次curl 调用上,例如:

curl http://example.com/?update_=1 http://example.com/?update_=2

According to the curl man page:

You can specify any amount of URLs on the command line. They will be
fetched in a sequential manner in the specified order.

So the simplest and most efficient (curl will send them all down a single TCP connection [those to the same origin]) approach would be put them all on a single invocation of curl e.g.:

curl http://example.com/?update_=1 http://example.com/?update_=2
绻影浮沉 2024-09-13 20:37:38

这里没有提到的另一个重要方法是使用相同的 TCP 连接来处理多个 HTTP 请求,并且为此使用一个curl 命令。

这对于节省网络带宽、客户端和服务器资源以及总体上使用多个curl命令的需要非常有用,因为curl默认情况下会在到达命令结束时关闭连接。

对于运行 Web 应用程序的标准客户端来说,保持连接打开并重用它是很常见的。

curl 版本 7.36.0 开始,--next-: 命令行选项允许链接多个请求,并且可以在命令中使用- 行和脚本。

例如:

  • 在同一个 TCP 连接上发送多个请求:

curl http://example.com/?update_=1 -: http://example.com/foo

  • 在同一连接上发送多个不同的 HTTP 请求:

curl http://example.com/?update_=1 -: -d "我正在发布此字符串" http://example.com/?update_=2

  • 使用不同的curl发送多个HTTP请求每个请求的选项:

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 将重置所有本地选项,只有全局选项的值会保留到 -:, --next 之后的操作中
操作说明。全局选项包括 -v、--verbose、--trace、
--trace-ascii 和 --fail-early。

例如,您可以在单个命令中同时执行 GET 和 POST
行:

curl www1.example.com --next -d postthis www2.example.com

在 7.36.0 中添加。

Another crucial method not mentioned here is using the same TCP connection for multiple HTTP requests, and exactly one curl command for this.

This is very useful to save network bandwidth, client and server resources, and overall the need of using multiple curl commands, as curl by default closes the connection when end of command is reached.

Keeping the connection open and reusing it is very common for standard clients running a web-app.

Starting curl version 7.36.0, the --next or -: command-line option allows to chain multiple requests, and usable both in command-line and scripting.

For example:

  • Sending multiple requests on the same TCP connection:

curl http://example.com/?update_=1 -: http://example.com/foo

  • Sending multiple different HTTP requests on the same connection:

curl http://example.com/?update_=1 -: -d "I am posting this string" http://example.com/?update_=2

  • Sending multiple HTTP requests with different curl options for each request:

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

From the curl manpage:

-:, --next

Tells curl to use a separate operation for the following URL and
associated options. This allows you to send several URL requests, each
with their own specific options, for example, such as different user
names or custom requests for each.

-:, --next will reset all local options and only global ones will have their values survive over to the operation following the -:, --next
instruction. Global options include -v, --verbose, --trace,
--trace-ascii and --fail-early.

For example, you can do both a GET and a POST in a single command
line:

curl www1.example.com --next -d postthis www2.example.com

Added in 7.36.0.

独自←快乐 2024-09-13 20:37:38

我迟到了 13 年,但与这里的所有其他答案相比,我有一些新的东西要补充。

我发现您的网址末尾有一个数字。我最近遇到了同样的问题,数字是从 0 到 13 的连续数字。以下是我如何用一行解决它:

curl http://example.com/?update_=[0-13]

例如,如果您只想要偶数,则可以指定跳转:

curl http://example.com/?update_=[0-13:2]

这是逐字记录卷曲的联机帮助页:

URL 语法与协议相关。您可以在 RFC 3986 中找到详细说明。

您可以通过在大括号内编写部分集并引用 URL 来指定多个 URL 或 URL 的一部分,如下所示:

“http://site.{一,二,三}.com”

或者您可以使用 [] 获取字母数字系列的序列,如下所示:

“ftp://ftp.example.com/file[1-100].txt”

“ftp://ftp.example.com/file[001-100].txt”(带前导零)

“ftp://ftp.example.com/file[az].txt”

不支持嵌套序列,但您可以相邻使用多个序列:

“http://example.com/archive[1996-1999]/vol[1-4]/part{a,b,c}.html”

I'm 13 years late to the party, but I have something new to add compared to all other answers here.

I realized you have a number at the end of the URL. I recently face the same issue and the number was a running number from 0 to 13. Here is how I solved it in one single line:

curl http://example.com/?update_=[0-13]

if you for example only want the even numbers, you can specify the jump:

curl http://example.com/?update_=[0-13:2]

Here is verbatim from the manpage of curl:

The URL syntax is protocol-dependent. You find a detailed description in RFC 3986.

You can specify multiple URLs or parts of URLs by writing part sets within braces and quoting the URL as in:

"http://site.{one,two,three}.com"

or you can get sequences of alphanumeric series by using [] as in:

"ftp://ftp.example.com/file[1-100].txt"

"ftp://ftp.example.com/file[001-100].txt" (with leading zeros)

"ftp://ftp.example.com/file[a-z].txt"

Nested sequences are not supported, but you can use several ones next to each other:

"http://example.com/archive[1996-1999]/vol[1-4]/part{a,b,c}.html"

谁对谁错谁最难过 2024-09-13 20:37:38

我认为这使用了更多本机功能

//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

I think this uses more native capabilities

//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
英雄似剑 2024-09-13 20:37:38

按所需顺序编写一个包含两个curl请求的脚本并通过cron运行它,例如

#!/bin/bash
curl http://mysite.com/?update_=1
curl http://mysite.com/?the_other_thing

Write a script with two curl requests in desired order and run it by cron, like

#!/bin/bash
curl http://mysite.com/?update_=1
curl http://mysite.com/?the_other_thing
慵挽 2024-09-13 20:37:38

这将执行您想要的操作,使用输入文件,并且

#!/bin/bash
IFS=

输入文件中每行一个条目的速度超级快,它将按照输入文件的顺序

将其保存为whatever.sh并使其可执行

\n' file=/path/to/input.txt lines=$(cat ${file}) for line in ${lines}; do curl "${line}" done IFS="" exit ${?}

输入文件中每行一个条目的速度超级快,它将按照输入文件的顺序

将其保存为whatever.sh并使其可执行

This will do what you want, uses an input file and is super fast

#!/bin/bash
IFS=

one entry per line on your input file, it will follow the order of your input file

save it as whatever.sh and make it executable

\n' file=/path/to/input.txt lines=$(cat ${file}) for line in ${lines}; do curl "${line}" done IFS="" exit ${?}

one entry per line on your input file, it will follow the order of your input file

save it as whatever.sh and make it executable

轻拂→两袖风尘 2024-09-13 20:37:38

你也可以使用括号 {}

假设你想卷曲

: wildfly_datasources_jdbc_total

b. wildfly_datasources_jdbc_currently

curl http://127.0.0.1:9990/metrics/vendor/wildfly_datasources_jdbc_{total,currently}

you can also use brackets {}

Let's say you want to curl:

a. wildfly_datasources_jdbc_total

b. wildfly_datasources_jdbc_currently

curl http://127.0.0.1:9990/metrics/vendor/wildfly_datasources_jdbc_{total,currently}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文