I have a parallelizable for loop in bash and would like to limit the number of jobs that are run in parallel. The script looks like this:
#!/usr/bin/env bash
num_cores=25
num_jobs=100
for ((i = 0; i < num_jobs; i++)); do
while read -r -a curr_jobs < <(jobs -p -r) \
&& ((${#curr_jobs[@]} >= num_cores)); do
wait -n
done
NAME=job_$i
screen -S $NAME -d -m bash -c "my bash command"
done
The script is based on the answer to a similar question on stackoverflow. The difference, though, is that Python
is called in that answer while I call screen
in my loop. It looks like this solution is not compatible with screen
for some reason that I am not aware of.
How can I modify my script to limit the number of parallel screen
sessions?
Are there easier/better solutions to this problem?