Abijith Kp
Abijith Kp

Reputation: 199

Running process in background

for i in `cat input`
    do
    youtube-dl $i &&
done

Here input file contains a list of YouTube video links. I expected this code to do a parallel download of the videos from the list. But the job was completed one after the other. Is it a problem with the code?

Upvotes: 1

Views: 291

Answers (3)

Aleks-Daniel Jakimenko-A.
Aleks-Daniel Jakimenko-A.

Reputation: 10663

for i in `cat input`; do
    youtube-dl "$i" &
done

Also, if you don't want to spam youtube with too many simultaneous downloads, you can use this:

cat file.txt | xargs -d '\n' -L 1 -P 5 youtube-dl

This code will be downloading only 5 videos at the same time. I am pretty sure that it is exactly what you're trying to do. This code will also work for links that contain spaces.

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185530

You have an extra &, so :

for i in $(< input); do
    youtube-dl "$i" &
done

To go further

  • the && operator is used in boolean tests, like : ((1 == 1)) && echo "1 == 1" || echo "1 != 1"
  • always protect your variables with double quotes to prevent bash word splitting !
  • the backticks are deprecated in 2013 in favor of the $( ) form. It's more readable, and easy to nest.

Upvotes: 1

user000001
user000001

Reputation: 33367

Use the & symbol instead of the && to run a process in the background. The script should look like this:

while read -r i
do
    youtube-dl "$i" &
done < "input"
wait ## Optional if you want the script to wait for all videos to download before exiting

Upvotes: 1

Related Questions