我正在尝试创建一个bash脚本,该脚本将运行多个长期存在的脚本-所有这些脚本都将在脚本结尾处终止。例如:
x.sh:
while :; do sleep 10 && echo '10 seconds passed'; done
y.sh的pseduo代码:
./x.sh [anchored&]
./x.sh [anchored&]
./x.sh [anchored&]
然后在外壳中,应该可以执行以下操作:
$ ./y.sh
# after 10 seconds
10 seconds passed
10 seconds passed
10 seconds passed
# after another 10 seconds
10 seconds passed
10 seconds passed
10 seconds passed
^[CTRL-C]
$ # all scripts are now canceled
当然,在我的实际用例中,我没有多次运行相同的脚本(x.sh),而是运行了不同的命令。
这是您可以使用的快速解决方案。
#!/bin/bash
trap terminate SIGINT
terminate(){
pkill -SIGINT -P $$
exit
}
#the rest of your code goes here
./x.sh &
./x.sh &
wait
trap
按下terminate
时将执行功能CTRL-C
。该函数将pkill
在所有父进程PID
是PID
发送信号的脚本的当前进程上执行SIGINT
。
将wait
在脚本确保年底,该脚本保持,而儿童进程正在运行运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句