Parallel Download Processes

1

Using a Bash terminal, how can I have 5 ± 2 concurrent cURL processes downloading at any time in a script, until there aren't any more links?

Pseudocode:

Links = {a.txt, b.txt, c.txt, d.txt ... x.txt, y.txt, z.txt}

Loop URLS in Links
    if (less than 5 cURL processes)
        cURL URL
    else
        wait until 1 cURL process stopped

MZhQfmpXFz3WZ9h

Posted 2017-04-28T00:33:33.460

Reputation: 13

Answers

1

Try this as a starting point (its untested)

#! /bin/bash
# LINKS is a list of space separated links use text below, otherwise
# replace first uncommented line below with
# for each in `cat $LINKS`

for each in $LINKS
do 
    count=`ps waux | grep "curl" | wc -l`
    until [ $count -gt 4 ]
    do
        count=`ps | grep "curl" | wc -l`
        sleep 1
    done

    curl $each &
    fi

done

davidgo

Posted 2017-04-28T00:33:33.460

Reputation: 49 152

What is the use of the ampersand (&) near "fi"? – MZhQfmpXFz3WZ9h – 2017-04-28T02:57:06.213

Also, shouldn't it be a -lt rather than -gt? – MZhQfmpXFz3WZ9h – 2017-04-28T03:04:52.927

The & means put the process in the background. You are welcome to execute the command using $() if thats how you prefer to do it - I wrote this script as a starting point. the -gt is correct because the script is waiting if there is more then 4 processes and only executing the next version when there are no longer more then 4 processes. (IE the logic is, by necessity the opposite to your pseudo-code in this respect). Please note that SU is not a script writing service and when we provide scripts its by way of demonstration of how to proceed, its not cast in stone. – davidgo – 2017-04-28T03:31:24.990