This is a function to run multiple jobs in parallel, but with a max number of parallel processes. New jobs will be run only if some previous jobs end and free up resources.
Credit to GPT-4.
First declare the following function in bash
function wait_for_jobs() {
local num_jobs=$1
while true; do