gpt4 book ai didi

bash - 如何终止所有子shell进程?

转载 作者:行者123 更新时间:2023-11-29 08:42:41 25 4
gpt4 key购买 nike

我有一个 bash 脚本来测试服务器在负载下的表现。

num=1
if [ $# -gt 0 ]; then
num=$1
fi
for i in {1 .. $num}; do
(while true; do
{ time curl --silent 'http://localhost'; } 2>&1 | grep real
done) &
done

wait

当我按下 Ctrl-C 时,主进程退出,但后台循环继续运行。我如何让他们全部退出?或者是否有更好的方法来生成可配置数量的并行执行的逻辑循环?

最佳答案

这是一个更简单的解决方案——只需在脚本顶部添加以下行:

trap "kill 0" SIGINT

杀死 0 将信号发送到当前进程组中的所有进程。

关于bash - 如何终止所有子shell进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8363519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com