在 bash 中并行执行 curl 请求

Jus*_*tin 23 multi-core smp wget curl

从 bash 脚本执行 5 个curl请求的最佳方法是什么parallel?出于性能原因,我无法串行运行它们。

Ant*_*hen 34

在命令后使用“&”使进程后台运行,并使用“wait”等待它们完成。如果您需要创建子 shell,请在命令周围使用 '()'。

#!/bin/bash

curl -s -o foo http://example.com/file1 && echo "done1" &
curl -s -o bar http://example.com/file2 && echo "done2" & 
curl -s -o baz http://example.com/file3 && echo "done3" &

wait
Run Code Online (Sandbox Code Playgroud)


小智 10

xargs 有一个“-P”参数来并行运行进程。例如:

wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Run Code Online (Sandbox Code Playgroud)

参考:http : //www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget


Den*_*ker 6

我将gnu parallel用于这样的任务。

  • 你能提供一个用`gnu parallel`调用`curl`的例子吗? (4认同)