Bash 如何杀死孙子进程
在BASH中杀害儿童看起来很容易。 但我的脚本让孩子们在UNIX传送带里。 而这种标准技术不起作用 我有这样一个BASH脚本Bash 如何杀死孙子进程,bash,Bash,在BASH中杀害儿童看起来很容易。 但我的脚本让孩子们在UNIX传送带里。 而这种标准技术不起作用 我有这样一个BASH脚本 ls -1 | { while read line ; do python -c 'while 1: pass' & done ; } while : ; do sleep 1 ; done 父进程应等待Ctrl+C或其他信号停止所有后台进程 执行无限循环的子进程 存在以下问题: jobs -p # pri
ls -1 | { while read line ; do
python -c 'while 1: pass' &
done ; }
while : ; do sleep 1 ; done
父进程应等待Ctrl+C或其他信号停止所有后台进程
执行无限循环的子进程
存在以下问题:
jobs -p # print nothing so trap hook cannot kill anybody
第二个是在花括号内定义的陷阱根本不被调用。
在这种情况下,无限循环也在大括号内
我有一个万不得已的解决方案,把pids从美元!进入tmp文件,但
对我来说很麻烦
尝试2
我已经用下面的新建议重写了代码,但仍然没有成功
#!/bin/bash
echo "my pid is $$"
exit_func() {
echo "pid in exit $(echo $$)"
echo "ps $(ps --no-headers --ppid $$ -o pid)"
echo "pgrep $(pgrep -P $$)"
echo "pstree $(pstree -p $$)"
}
trap 'exit_func' EXIT
ls -1 / | {
while read a ; do
python -c 'while 1: pass' &
echo -n " $!"
done
}
while : ; do : ; done
Utils-ps、pgrep和pstree无法看到python子级
[dan@localhost]$ ./testtrap.sh
my pid is 17848
17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 C-c C-\
cpid in exit 17848
ps 17875
pgrep 17877
pstree testtrap.sh(17848)---testtrap.sh(17879)---pstree(17880)
[dan@localhost]$ ps
PID TTY TIME CMD
17851 pts/2 00:00:12 python
17852 pts/2 00:00:12 python
17853 pts/2 00:00:12 python
17854 pts/2 00:00:12 python
17855 pts/2 00:00:12 python
17856 pts/2 00:00:14 python
17857 pts/2 00:00:12 python
17858 pts/2 00:00:15 python
17859 pts/2 00:00:12 python
17860 pts/2 00:00:12 python
17861 pts/2 00:00:15 python
17862 pts/2 00:00:12 python
17863 pts/2 00:00:12 python
17864 pts/2 00:00:12 python
17865 pts/2 00:00:12 python
17866 pts/2 00:00:14 python
17867 pts/2 00:00:12 python
17868 pts/2 00:00:12 python
17869 pts/2 00:00:12 python
17870 pts/2 00:00:15 python
17871 pts/2 00:00:12 python
17872 pts/2 00:00:12 python
17873 pts/2 00:00:12 python
17959 pts/2 00:00:00 ps
27560 pts/2 00:00:00 bash
[dan@localhost]$
考虑到无限循环可能运行得很快,键盘CTRL C丢失了(这确实发生了,但是有时如果你很快地按下CTRL C 10X,它们中的一个将会通过)。(哎呀,我看到你的测试中已经有了一个
sleep 1
,但也许你想在循环的顶部添加类似echo“第$((I++)次)循环”
)的内容。)祝你好运。请看我修改后的评论。