title: How to parallelize a for loop that includes screen while limiting the number of processes?
body: <p>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:</p>
  
  <pre><code>#!/usr/bin/env bash
  
  num_cores=25
  num_jobs=100
  
  for ((i = 0; i &lt; num_jobs; i++)); do
      while read -r -a curr_jobs &lt; &lt;(jobs -p -r) \
          &amp;&amp; ((${#curr_jobs[@]} &gt;= num_cores)); do
          wait -n
      done
      NAME=job_$i
      screen -S $NAME -d -m bash -c "my bash command"
  done
  </code></pre>
  
  <p>The script is based on <a href="https://stackoverflow.com/a/38774538/4231538">the answer</a> to a similar question on stackoverflow. The difference, though, is that <code>Python</code> is called in that answer while I call <code>screen</code> in my loop. It looks like this solution is not compatible with <code>screen</code> for some reason that I am not aware of.</p>
  
  <p>How can I modify my script to limit the number of parallel <code>screen</code> sessions?</p>
  
  <p>Are there easier/better solutions to this problem?</p>