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]
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.
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.
Forking / Multi-Threaded Processes | Bash
#!/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 & //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 <"$file"
Please be cognizant that I am newbie and do not know much about the shell process.
Will appreciate any directions and feedback.
Thanks