【发布时间】:2018-05-03 06:25:34
【问题描述】:
我需要从一个文件中读取数百个 id,并将它们作为参数传递给另一个 shell 脚本,以作为单独的子请求生成。 [完成]
但我们不能产生超过 6 个子请求,即在给定时间点运行的请求不能超过 6 个。
我浏览了这个站点(下面给出了参考资料)和其他站点,并了解到您可以使用 $!但距离实现它还很遥远,因为我不知道如何将它们存储在一个数组中,并在生成的过程完成后将其删除。
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"
请注意,我是新手,对shell进程了解不多。
将不胜感激任何指示和反馈。
谢谢
【问题讨论】: