title: Bash script to read ids from file and spawn process in a controlled manner
body: <p>I need to read hundreds of ids from a file and pass those to another shell script as parameters to be spawned as separate child requests. [Done]</p>
  
  <p>But we cannot spawn more than 6 child requests i.e., not more than 6 requests can be running at a given point in time.</p>
  
  <p>I have gone through this site (references given below) and others and came to know that you can get the PID of the spawned process using $! but am faraway from implementing it as I do not know how to store them in an array and delete it once the spawned process is complete.</p>
  
  <p><a href="https://stackoverflow.com/questions/1455695/forking-multi-threaded-processes-bash">Forking / Multi-Threaded Processes | Bash</a></p>
  
  <p><a href="https://stackoverflow.com/questions/356100/how-to-wait-in-bash-for-several-subprocesses-to-finish-and-return-exit-code-0">How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?</a></p>
  
  <pre><code>#!/bin/bash
  file="/usr/share/nginx/html/cron/userids.txt" //file containing the userids that needs to be spawned
  
  MAXCOUNT=6 //maximum number of child request that can be spawned
  
  while IFS= read -r line
  do
      #submit a background job here
  
      sh fetch.sh $line &amp; //this is the shell script that needs to be submitted
  
      //check if the spawned request count is less than MAXCOUNT
      //If it is then wait for one or more request to finish 
      //If the number of child requests is less than MAXCOUNT then spawn another request 
      //if all the lines are read then wait till all the child process completes and then exit
  
  done &lt;"$file"
  </code></pre>
  
  <p>Please be cognizant that I am newbie and do not know much about the shell process.</p>
  
  <p>Will appreciate any directions and feedback.</p>
  
  <p>Thanks</p>