There have been some similar questions, but my problem is not \"run several programs in parallel\" - which can be trivially done with parallel
or xargs
Solution to run multi-line commands in parallel:
for ...your_loop...; do
if test "$(jobs | wc -l)" -ge 8; then
wait -n
fi
{
any bash commands here
} &
done
wait
In your case:
for i in "${list[@]}"
do
for j in "${other[@]}"
do
if test "$(jobs | wc -l)" -ge 8; then
wait -n
fi
{
your
multi-line
commands
here
} &
done
done
wait
If there are 8 bash jobs already running, wait
will wait for at least one job to complete. If/when there are less jobs, it starts new ones asynchronously.
Benefits of this approach:
man
):parallel is slow at starting up - around 250 ms the first time and 150 ms after that. 3. Only needs
bash
to work.
Downsides:
wait
with fewer jobs than required. However, it will resume when at least one job completes, or immediately if there are 0 jobs running (wait -n
exits immediately in this case).&
) within the same bash script, you'll have fewer worker processes in the loop.