Scripting‎ > ‎Linux shell‎ > ‎

Control and run multiple processes in bash

Thank you for visiting this page, this page has been update in another link Control and run multiple processes in bash
Quite often, in shell environment, you want to run multiple processes to either fully utilize you CPU resources or other purpose.
In the begining time, I used the way like this
for i in `seq 1 100`
{
   mytask &   
sleep 1;
}

Also, tried this way

for i in `seq 1 100`
{
   mytask &   
   while  [ `ps -ef | grep mytask | grep -v grep |wc -l` -gt 3 ]   
do
sleep 1;
done
}

Similar way by using function

function max2 { while [ `jobs | wc -l` -gt 2 ] do sleep 1; done } cat ./jobs.lst | while read name ; do max2; mytask ${name} & done wait

Here is the easist way I found
find ./x* | xargs -n 1 -P 5 bzip2
or
for i in `seq 1 100` | xargs -n 1 -P 5 mytask

where, -n controls number of parameters to be processes
-P controls how many CPU/processes to run in parallel

Similarily, there is GNU parallel command you can use, but I haven't got the package installed yet, will try and let you know.


Comments