>我需要从一个文件中读取数百个id,并将它们作为参数传递给另一个shell脚本,作为单独的子请求生成。[完成]
但是我们不能生成超过 6 个子请求,即在给定时间点运行的请求不能超过 6 个。
我已经浏览了这个网站(下面给出的参考资料(和其他网站,并了解到您可以使用 $! 但离实现它很远,因为我不知道如何将它们存储在数组中并在生成过程完成后将其删除。
分叉/多线程进程 |砰砰��
当任何子进程以代码 !=0 结束时,如何在 bash 中等待多个子进程完成并返回退出代码 !=0?
#!/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"
请注意,我是新手,对外壳过程了解不多。
将不胜感激任何指示和反馈。
谢谢
你可以使用 GNU Parallel 来实现这一点:
parallel -j6 -a "$file" fetch.sh
它有很多选项来处理故障,进度条,日志记录等。
xargs
生成传递从 stdin 读取的参数的最大进程数
xargs -n 1 -P 6 fetch.sh < "$file"