83

I am getting a bunch of relatively small pages from a website and was wondering if I could somehow do it in parallel in Bash. Currently my code looks like this, but it takes a while to execute (I think what is slowing me down is the latency in the connection).

for i in {1..42}
do
    wget "https://www.example.com/page$i.html"
done

I have heard of using xargs, but I don't know anything about that and the man page is very confusing. Any ideas? Is it even possible to do this in parallel? Is there another way I could go about attacking this?

0

5 Answers 5

206

Much preferrable to pushing wget into the background using & or -b, you can use xargs to the same effect, and better.

The advantage is that xargs will synchronize properly with no extra work. Which means that you are safe to access the downloaded files (assuming no error occurs). All downloads will have completed (or failed) once xargs exits, and you know by the exit code whether all went well. This is much preferrable to busy waiting with sleep and testing for completion manually.

Assuming that URL_LIST is a variable containing all the URLs (can be constructed with a loop in the OP's example, but could also be a manually generated list), running this:

echo $URL_LIST | xargs -n 1 -P 8 wget -q

will pass one argument at a time (-n 1) to wget, and execute at most 8 parallel wget processes at a time (-P 8). xarg returns after the last spawned process has finished, which is just what we wanted to know. No extra trickery needed.

The "magic number" of 8 parallel downloads that I've chosen is not set in stone, but it is probably a good compromise. There are two factors in "maximising" a series of downloads:

One is filling "the cable", i.e. utilizing the available bandwidth. Assuming "normal" conditions (server has more bandwidth than client), this is already the case with one or at most two downloads. Throwing more connections at the problem will only result in packets being dropped and TCP congestion control kicking in, and N downloads with asymptotically 1/N bandwidth each, to the same net effect (minus the dropped packets, minus window size recovery). Packets being dropped is a normal thing to happen in an IP network, this is how congestion control is supposed to work (even with a single connection), and normally the impact is practically zero. However, having an unreasonably large number of connections amplifies this effect, so it can be come noticeable. In any case, it doesn't make anything faster.

The second factor is connection establishment and request processing. Here, having a few extra connections in flight really helps. The problem one faces is the latency of two round-trips (typically 20-40ms within the same geographic area, 200-300ms inter-continental) plus the odd 1-2 milliseconds that the server actually needs to process the request and push a reply to the socket. This is not a lot of time per se, but multiplied by a few hundred/thousand requests, it quickly adds up.
Having anything from half a dozen to a dozen requests in-flight hides most or all of this latency (it is still there, but since it overlaps, it does not sum up!). At the same time, having only a few concurrent connections does not have adverse effects, such as causing excessive congestion, or forcing a server into forking new processes.

14
  • 5
    This is definitely the best way, since it uses xargs, a universal tool, and this method can be applied to many other commands.
    – SineSwiper
    Aug 10, 2012 at 21:00
  • 7
    When downloading multiple files over HTTP, wget can reuse the HTTP connection thanks to the Keep-Alive mechanic. When you launch a new process per each file, this mechanism cannot be used and the connection (TCP triple way handshake) has to be established again and again. So I suggest bumping up the -n parameter to about 20 or so. In default configuration Apache HTTP server will serve only up to 100 requests in one kept-alive session, so there is probably no point in going over a hundered here.
    – user7610
    May 17, 2013 at 22:07
  • 2
    Great answer, but what if I want to pass two variable values to wget? I want to specify the destination path as well as the URL. Is this still possible with the xargs technique?
    – Ricky
    May 8, 2014 at 23:09
  • 1
    @DomainsFeatured There no option for xargs that does this. This is one of the reasons for developing GNU Parallel in the first place.
    – Ole Tange
    Oct 1, 2016 at 21:14
  • 2
    @Justin You could just use cat ./urls | xargs -n 1 -P 8 wget [...], or even better xargs -a ./urls -n 1 -P 8 wget [...], instead of reading the file into a variable. Jun 1, 2017 at 1:40
65

Just running the jobs in the background is not a scalable solution: If you are fetching 10000 urls you probably only want to fetch a few (say 100) in parallel. GNU Parallel is made for that:

seq 10000 | parallel -j100 wget https://www.example.com/page{}.html

See the man page for more examples: http://www.gnu.org/software/parallel/man.html#example__download_10_images_for_each_of_the_past_30_days

3
  • Sorry, I have nothing to download now, but I'll have to do it in the future for sure. Assuming I run seq 30 | parallel -j5 mkdir /tmp/{} Should it create 30 folders /tmp/1 /tmp/2 etc. ? If so, it doesn't do it on my system.
    – ka3ak
    Sep 29, 2018 at 14:11
  • @ka3ak You may have found a bug. Please follow: gnu.org/software/parallel/man.html#REPORTING-BUGS
    – Ole Tange
    Sep 29, 2018 at 18:53
  • 1
    @OleTange It seems there was another tool with the same name which was pre-installed on my system. It even had the option -j for jobs. I just ran sudo apt install parallel to install the right one.
    – ka3ak
    Sep 30, 2018 at 6:36
9

You can use -b option:

wget -b "https://www.example.com/page$i.html"

If you don't want log files, add option -o /dev/null.

-o FILE  log messages to FILE.
2
  • 1
    No, it's ok - check man page ('-o logfile...').
    – uzsolt
    Nov 25, 2013 at 11:52
  • Sorry, I did not read correctly. I thought you said "if you do not want output files, add option -o". Because I did that and ended up with hundreds of thousands of files in /root. Thanks for clarifying.
    – ipruthi
    Nov 26, 2013 at 5:58
6

Adding an ampersand to a command makes it run in the background

for i in {1..42}
do
    wget "https://www.example.com/page$i.html" &
done
3

wget version 2 implements multiple connections.

https://github.com/rockdaboot/wget2

Not the answer you're looking for? Browse other questions tagged or ask your own question.