Reputation: 1131
I have a for
loop that will cycle through all the files in a directory. I want to run a process on each file (in this example echo
file name and sleep 5
). However I want to be able to run this 5 files at a time (in the background with &
). The problem is I can't figure out how to iterate $f
within the while
loop so that I don't end up processing the same file five times instead of five different files at the same time.
#!/bin/bash
maxjobs=5
for f in `ls /home/user/`
do
jobsrunning=0
while [ $jobsrunning -lt $maxjobs ]
do
echo "Converting file"$f
sleep 5 & #wait for 5 seconds
jobsrunning=$((jobsrunning + 1))
echo $jobsrunning
done
wait
done
Upvotes: 1
Views: 80
Reputation: 77079
You really just need to reset jobsrunning
, and I don't think you need the inner loop at all. It's just a condition.
#!/bin/bash
maxjobs=5
jobsrunning=0
for f in /home/user/*; do
if (( jobsrunning >= maxjobs )); then
wait
jobsrunning=0
fi
echo "converting"
sleep 5 & # wait for how many seconds?
(( jobsrunning++ ))
echo "$jobsrunning"
done
wait
That said, this sounds like a job for parallel.
Upvotes: 3