Hi.
In my bash script I have a loop which downloads a few files in the background with 'wget'.
How can I make my script wait until all downloads are complete before it goes on?

I guess I have to use the 'wait' command after the loop in some way?
But I might need to get ahold of the process ID's?
I'm pretty new to this.

an example of the loop:

for I in 01 02 03
do
    wget -b -a download.log this.is.an.url/$I.txt
done

Recommended Answers

All 4 Replies

You can either not put wget in the background (drop the -b flag) or you can issue a wait (without arguments) directly after your loop.

I am guessing that you want all 3 to kick off at the same time then carry on once the last wget completes...
For that try:

answ=0
until [[ $answ -gt 0 ]]
do
ps -ef | grep wget | grep -v grep
answ=$?
done

(You will want to modify the ps command to not output)
This will keep in the loop while the ps command finds a running instance of wget. Once wget ends and ps does not return anything then the script will continue.

Hope this helps...
John

Why would you give such a convoluted solution? Particularly one that is not even complete?
The whole point of wait is to wait for child processes. Why implement half of a fix that already exists?

Good point...Sorry, I misread your post...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.