Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parallelise a while loop in bash?

I have the following while loop that runs in parallel. ( The logProcess is a function that I define earlier in my script. )

while read LINE; do
    logProcess $LINE &
done <<< "$ELS_LOGS"
wait

I need to find a way to limit the number of processes running. I know there are parallel processes running. How do I convert the loop to use that command?

like image 889
Matthew Bullock Avatar asked Oct 06 '17 09:10

Matthew Bullock


1 Answers

May I recommend GNU Parallel to you. Your entire script would become:

parallel -a "$ELS_LOGS" logProcess

If logProcess is a function defined within your script, you will need to export it first before running GNU Parallel, like this:

export -f logProcess

Then if you want, say, 8 running at a time, you simply do:

parallel -j 8 -a "$ELS_LOGS" logProcess

If you want to see what it would do, without actually doing anything:

parallel --dry-run ...

If you want a progress bar, or an ETA:

parallel --eta ...
parallel --bar ...
like image 137
Mark Setchell Avatar answered Nov 01 '22 11:11

Mark Setchell