parallel execution in bash scripts
Recently, i had a problem - i needed to run several utilities in parallel in bash script, wait till they finish and then gather the information they produced. It’s sooooooooo easy!
Bash has ‘wait’ command which… (surprisingly) waits for all (or certain) background processes to finish. Knowing that, the rest is easy.
Here’s a bash script which simulates utilities with ’sleep’ command.
nParallelTasks=10
nStartedTasks=0
for i in `seq 1 100`
do
echo "-- $i --"
sleep 5&
nStartedTasks=$(($nStartedTasks + 1))
if [[ "$nStartedTasks" == "$nParallelTasks" ]]
then
echo "waiting"
wait
nStartedTasks=0
fi
done

