为什么在Linux中杀死父进程后子进程还活着? [英] Why child process still alive after parent process was killed in Linux?
问题描述
有人告诉我,当你在 linux 中杀死一个父进程时,子进程就会死亡.
但我对此表示怀疑.所以我写了两个 bash 脚本,其中 father.sh
会调用 child.sh
Someone told me that when you killed a parent process in linux, the child would die.
But I doubt it. So I wrote two bash scripts, where father.sh
would invoke child.sh
这是我的脚本:
现在我运行bashfather.sh
,你可以查看ps -alf
Now I run bash father.sh
, you could check it ps -alf
然后我通过kill -9 24588
杀死了father.sh
,我猜子进程应该被终止,但不幸的是我错了.
Then I killed the father.sh
by kill -9 24588
, and I guessed the child process should be terminated but unfortunately I was wrong.
谁能解释一下为什么?
谢谢
推荐答案
不,当你单独杀死一个进程时,它不会杀死孩子.
No, when you kill a process alone, it will not kill the children.
如果您希望给定组的所有进程都接收信号,则必须将信号发送到进程组
You have to send the signal to the process group if you want all processes for a given group to receive the signal
例如,如果您的父进程 ID 的代码为 1234,则您必须指定 parentpid,添加符号减号,后跟您的父进程 ID:
For example, if your parent process id has the code 1234, you will have to specify the parentpid adding the symbol minus followed by your parent process id:
kill -9 -1234
否则,孤儿将链接到 init
,如您的第三个屏幕截图所示(孩子的 PPID 已变为 1).
Otherwise, orphans will be linked to init
, as shown by your third screenshot (PPID of the child has become 1).
这篇关于为什么在Linux中杀死父进程后子进程还活着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!