'run parallel bash command independently to each other [duplicate]

I have a cluster of 40 nproc where my software uses ten cores at a time. I want to run 4 task at a time from single bash script (job script), and whenever one completes it, start another one. The loop needs to run as per the input files (approx 600). Each command executes in its specific folder for n times.

currently, I am using the following script

#! /bin/bash

for f in lib1/*.png; do
for g in lib2/*.png; do
for h in lib3/*.png; do
for i in lib4/*.png; do
        ./MySoftyware --input $f --out $f_out.png &
P1=$!
        ./MySoftyware --input $g --out $g_out.png &
P2=$!
        ./MySoftyware --input $h --out $h_out.png &
P3=$!
        ./MySoftyware --input $i --out $i_out.png &
P4=$!
wait $P1 $P2 $P3 $P4
done
done
done
done

The issue is that process only starts again when all four are completed, but I want it to execute on its next available file as soon as one is completed from the four commands. e.g., if $f is completed before $i, it should execute the next file in $f and not wait for the completion of $i



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source