i have a while loop reading lines from a $hosts
while read line
do
ip=$line
check
done < $hosts
my question is can I use some way to speed this up or run the check on 10 hosts at a time and each check is on a different IP and finish when all IP in $host
have been checked?
Thanks
Use GNU Parallel:
You can start multiple processes, each calling the function
check
and wait for them to finish.Whether this increases the speed depends on available processors and the function
check
's implementation. You have to ensure there's no data dependency incheck
between iterations.You can send tasks to the background by
&
If you intend to wait for all of them to finish you can use thewait
command:You can get the
pid
of the given task started in the background if you want to wait for one (or few) specific tasks.On note though: the background processes can mess up the output as they will run asynchronously. You might want to use a named pipe to collect the output from them.
edit
As asked in the comments there might be a need for limiting the background processes forked. In this case you can keep track of how many background processes you've started and communicate with them through a named pipe.