kill -s SIGTERM仅杀死父进程和一级子进程



我一直在做一些实验,写一个命令来递归地杀死父节点及其所有的子节点。我有一个脚本如下

parent.sh:

#!/bin/bash
/home/oracle/child.sh &
sleep infinity

child.sh:

#!/bin/bash
sleep infinity

使用

启动命令
su oracle -c parent.sh &

我看到如下所示的进程树

[root@source ~]# ps -ef | grep "/home/oracle"
root     14129  1171  0 12:39 pts/1    00:00:00 su oracle -c /home/oracle/parent.sh
oracle   14130 14129  0 12:39 ?        00:00:00 /bin/bash /home/oracle/parent.sh
oracle   14131 14130  0 12:39 ?        00:00:00 /bin/bash /home/oracle/child.sh

当我使用kill -s SIGTERM 14129发送sigterm到14129时,它似乎杀死了14129,然后14130也立即下降;但是14131会持续很长时间。最后一关的孩子似乎已经被重新养育并变成了僵尸。

oracle   14131     1  0 12:39 ?        00:00:00 /bin/bash /home/oracle/child.sh
  1. 如果kill不终止任何子进程,为什么当我发送SIGTERM到14129时14130被杀死?
  2. 如果kill可以杀死子进程,为什么它只向下一层?这里的行为有保证吗?

pilcrow提供的相关部分是这样的:

信号前

Upon receiving either SIGINT, SIGQUIT or SIGTERM, su terminates
its child and afterwards terminates itself with the received
signal. 
>> The child is terminated by SIGTERM, 
>> (then) after unsuccessful attempt (to kill with SIGTERM) and 
>> (after) 2 seconds of delay (,) the child is (then) killed 
>> by SIGKILL [a second, harsher method].

更苛刻的方法SIGKILL阻止该子进程不会试图杀死自己的子进程,因此处于僵尸状态。

我自己没有使用过,但似乎有些东西像

killall --process-group parent.sh

将杀死与父进程组关联的所有进程。sh";脚本。…不确定是否——wait";如果尝试终止的方法不被接受,将对您很有帮助。

最新更新