当一个父进程被“kill -9”杀死时,子进程也会被杀死吗?

时间:2020-12-07 20:25:02

One of my colleague told me this morning, when he killed supervisord by "kill -9", the subprocesses of supervisord is not killed.

我的一位同事今天早上告诉我,当他用“杀死-9”杀死监督时,监督的子过程没有被杀死。

He is quite sure about that, but I tried many times and did not find that happen.

他对此非常肯定,但我多次尝试并没有发现这种情况。

So when a parent process is killed by "kill -9", will linux ensure that it's sub-processes also been killed?

因此当父进程被“kill -9”杀死时,linux会确保它的子进程也被杀死吗?

5 个解决方案

#1


16  

You have to make the sub processes daemonic in order to have them killed when the father is killed (or dies), otherwise they are adopted by init(1).

你必须使子进程守护进程,以便在父亲被杀死(或死亡)时杀死它们,否则它们被init(1)采用。

#2


23  

No, child processes are not necessarily killed when the parent is killed.

不,当父母被杀时,子进程不一定被杀死。

However, if the child has a pipe open which it is writing to and the parent is reading from, it will get a SIGPIPE when it next tries to write to the pipe, for which the default action is to kill it. That is often what happens in practice.

但是,如果子进程正在写入并且父进程正在读取,则当下一次尝试写入管道时,它将获得一个SIGPIPE,默认操作是将其删除。这通常是在实践中发生的事情。

#3


8  

On UNIX, there is no enforced relation between parent and child process's lifetimes. Strictly speaking, process will only terminate when they call exit() or receive an unhandled signal for which default action is to terminate.

在UNIX上,父进程和子进程的生命周期之间没有强制关系。严格地说,进程只有在调用exit()或接收未处理的信号时才会终止,默认操作将终止。

However, an entire "foreground process group" in a "controlling terminal" can receive signals like SIGINT and SIGQUIT when the user hits ctrl-C, ctrl-\, etc. on that terminal. Specific behaviour is partly implemented by the login shell (with help from the tty driver). Details may be quite complicated: look here and here

但是,当用户点击该终端上的ctrl-C,ctrl- \等时,“控制终端”中的整个“前台进程组”可以接收SIGINT和SIGQUIT等信号。具体行为部分由登录shell实现(在tty驱动程序的帮助下)。细节可能相当复杂:看这里和这里

#4


-1  

If u close the terminal pid which is the parent process id of the process then terminal is closed. And when terminal is closed then all its processes also gets killed. But if u create a sub shell in shell then if u create any process and kill ppid of that process then only that sub shell kill and their child becomes orphans. Their parent becomes init and pid is 1.

如果你关闭作为进程的父进程id的终端pid,则关闭终端。当终端关闭时,它的所有进程也会被终止。但是,如果你在shell中创建一个子shell,那么如果你创建任何进程并杀死该进程的ppid,那么只有那个子shell会杀死他们的孩子成为孤儿。他们的父母变成了init,pid是1。

[trainee@SIPL ~]$ ps -ef | grep sleep trainee 3893 3870 0 10:55 pts/1 00:00:00 sleep 4000 trainee 3895 3788 0 10:55 pts/0 00:00:00 grep --color=auto sleep [trainee@SIPL ~]$ kill -9 3870 [trainee@SIPL ~]$ ps -ef | grep sleep trainee 3893 1 0 10:55 pts/1 00:00:00 sleep 4000 trainee 3906 3788 0 10:55 pts/0 00:00:00 grep --color=auto sleep

[实习生@ SIPL~] $ ps -ef | grep sleep trainee 3893 3870 0 10:55 pts / 1 00:00:00 sleep 4000 trainee 3895 3788 0 10:55 pts / 0 00:00:00 grep --color = auto sleep [trainee @ SIPL~] $ kill - 9 3870 [实习生@SIPL~] $ ps -ef | grep sleep trainee 3893 1 0 10:55 pts / 1 00:00:00 sleep 4000 trainee 3906 3788 0 10:55 pts / 0 00:00:00 grep --color = auto sleep

#5


-9  

You just need to know which process or service you wanna kill. In my case httpd is.

您只需要知道要杀死哪个进程或服务。在我的情况下,httpd是。

killall -9 httpd

It will kill all the child processes of httpd.

它会杀死httpd的所有子进程。

#1


16  

You have to make the sub processes daemonic in order to have them killed when the father is killed (or dies), otherwise they are adopted by init(1).

你必须使子进程守护进程,以便在父亲被杀死(或死亡)时杀死它们,否则它们被init(1)采用。

#2


23  

No, child processes are not necessarily killed when the parent is killed.

不,当父母被杀时,子进程不一定被杀死。

However, if the child has a pipe open which it is writing to and the parent is reading from, it will get a SIGPIPE when it next tries to write to the pipe, for which the default action is to kill it. That is often what happens in practice.

但是,如果子进程正在写入并且父进程正在读取,则当下一次尝试写入管道时,它将获得一个SIGPIPE,默认操作是将其删除。这通常是在实践中发生的事情。

#3


8  

On UNIX, there is no enforced relation between parent and child process's lifetimes. Strictly speaking, process will only terminate when they call exit() or receive an unhandled signal for which default action is to terminate.

在UNIX上,父进程和子进程的生命周期之间没有强制关系。严格地说,进程只有在调用exit()或接收未处理的信号时才会终止,默认操作将终止。

However, an entire "foreground process group" in a "controlling terminal" can receive signals like SIGINT and SIGQUIT when the user hits ctrl-C, ctrl-\, etc. on that terminal. Specific behaviour is partly implemented by the login shell (with help from the tty driver). Details may be quite complicated: look here and here

但是,当用户点击该终端上的ctrl-C,ctrl- \等时,“控制终端”中的整个“前台进程组”可以接收SIGINT和SIGQUIT等信号。具体行为部分由登录shell实现(在tty驱动程序的帮助下)。细节可能相当复杂:看这里和这里

#4


-1  

If u close the terminal pid which is the parent process id of the process then terminal is closed. And when terminal is closed then all its processes also gets killed. But if u create a sub shell in shell then if u create any process and kill ppid of that process then only that sub shell kill and their child becomes orphans. Their parent becomes init and pid is 1.

如果你关闭作为进程的父进程id的终端pid,则关闭终端。当终端关闭时,它的所有进程也会被终止。但是,如果你在shell中创建一个子shell,那么如果你创建任何进程并杀死该进程的ppid,那么只有那个子shell会杀死他们的孩子成为孤儿。他们的父母变成了init,pid是1。

[trainee@SIPL ~]$ ps -ef | grep sleep trainee 3893 3870 0 10:55 pts/1 00:00:00 sleep 4000 trainee 3895 3788 0 10:55 pts/0 00:00:00 grep --color=auto sleep [trainee@SIPL ~]$ kill -9 3870 [trainee@SIPL ~]$ ps -ef | grep sleep trainee 3893 1 0 10:55 pts/1 00:00:00 sleep 4000 trainee 3906 3788 0 10:55 pts/0 00:00:00 grep --color=auto sleep

[实习生@ SIPL~] $ ps -ef | grep sleep trainee 3893 3870 0 10:55 pts / 1 00:00:00 sleep 4000 trainee 3895 3788 0 10:55 pts / 0 00:00:00 grep --color = auto sleep [trainee @ SIPL~] $ kill - 9 3870 [实习生@SIPL~] $ ps -ef | grep sleep trainee 3893 1 0 10:55 pts / 1 00:00:00 sleep 4000 trainee 3906 3788 0 10:55 pts / 0 00:00:00 grep --color = auto sleep

#5


-9  

You just need to know which process or service you wanna kill. In my case httpd is.

您只需要知道要杀死哪个进程或服务。在我的情况下,httpd是。

killall -9 httpd

It will kill all the child processes of httpd.

它会杀死httpd的所有子进程。