Linux, programming, computers and life

March 3, 2008

parallel execution in bash scripts

Filed under: CLI, programming, linux

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

Technorati Tags: , , , , ,

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers