使用SIGKILL杀死父进程以及子进程 [英] killing Parent process along with child process using SIGKILL

查看:676
本文介绍了使用SIGKILL杀死父进程以及子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个shell脚本,在其中我有一个父进程,它有一个由sleep &命令创建的子进程.现在,我希望杀死父进程,以便子进程也将被杀死.我可以通过以下命令做到这一点:

I am writing one shell script in which I have parent process and it has child processes which are created by sleep & command. Now I wish to kill the parent process so that the child process will be also killed. I was able to do that this with below command:

trap "kill $$" SIGINT
trap 'kill -HUP 0' EXIT
trap 'kill $(jobs -p)' EXIT

这些命令与kill [parent_process_ID]命令一起使用,但是如果我使用kill -9 [parent_process_ID],则只有父进程将被杀死. 请进一步指导我实现此功能,以便当我使用任何命令终止父进程时,也应终止子进程.

These commands are working with kill [parent_process_ID] commands but if I use kill -9 [parent_process_ID] then only the parent process will be killed. Please guide me further to achieve this functionality so that when I kill parent process with any command then child process should be also killed.

推荐答案

单独杀死一个进程不会杀死其子进程.

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.

kill -9 -parentpid

否则,孤儿将链接到init.

孩子可以通过在prctl() syscall中指定选项PR_SET_PDEATHSIG来要求内核在父母去世时发出SIGHUP(或其他信号):

Child can ask kernel to deliver SIGHUP (or other signal) when parent dies by specifying option PR_SET_PDEATHSIG in prctl() syscall like this:

prctl(PR_SET_PDEATHSIG, SIGHUP);

有关详细信息,请参见man 2 prctl.

See man 2 prctl for details.

这篇关于使用SIGKILL杀死父进程以及子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆