I'm running a computationally heavy program on a list of files in bash. If I do them one at a time, I do not utilize my computer power, yet if I append the &
symbol to the command to run them in background processes, I'm running too many. What I'm looking for is a way to specify that I want n
processes to work through a particular list of items files. When one finishes, it moves on to another.
As a minimal example, here is some setup code to replicate my situation:
$ mkdir test
$ cd test
$for i in {1..1000}
>do
> echo "$i" >> $i.txt
> done
How would I use (say) 2 processes only to process this list of files so that the output in each file does some arbitrary operation to the number $i
(maybe add two or something) and then prints, done by process 1 or 2
, depending on whether process 1 or 2 did the operation?