7

I have a question. Studying processes management I observed a strange behavior, on a CentOS 7. I know that killing a parent process, the child processes are killed also. But not in the following case. I ran the command dd, just for example:

[root@server2 ~]# dd if=/dev/zero of=/dev/null &
[1] 1756

[root@server2 ~]# ps fax | grep -B2 dd
1737 pts/2    S      0:00         \_ su -
1741 pts/2    S      0:00             \_ -bash
1756 pts/2    R      1:18                 \_ dd if=/dev/zero of=/dev/null

After that I tried to kill (with SIGKILL signal) the parent process, that is the bash, but this action doesn't kill the dd process:

[root@server2 ~]# kill -9 1741
Killed
[user@server2 ~]#

The shell terminates but as you can see in the top command output, the dd process is still working:

PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
1756 root      20   0  107948    612    512 R 99.9  0.1  10:06.98 dd

Do you have any idea about it please?

3 Answers 3

13

By default killing a parent process does not kill the children processes.

I suggest you look for other questions about how to kill both the parent and child using the process group (a negative PID).

A good answer about how to do this in detail can be found at Process descendants

1

I was in a similar situation and found the answer.

TLDR;

try: kill -2 <parent_pid>

You will know if this method will work for you, if while running your script and you press Ctrl+C would kill the parent and all spawned processes.

The reason for this is the different signals that kill can send. By default kill <pid> will send 15 <SIGTERM> signal. But the regular signal when pressing Ctrl+C is not 15 <SIGTERM> it is in fact 2 <SIGINT>.

So if you kill -2 <pid> this will kill your parent process as if having interrupted it with Ctrl+C

source

1
  • I don't think this is quite correct. Ctrl-C kills all the processes because the of terminal magic. kill -2 will still only send it to one process.
    – Timmmm
    Commented Oct 10, 2023 at 10:02
1

Please adjust to your own needs, especially on the kill command:

function kill_recurse() {
    cpids=`pgrep -P $1|xargs`
    for cpid in $cpids;
    do
        kill_recurse $cpid
    done
    echo "killing $1"
    kill -9 $1
}

Example usage would be: kill_recurse <my_parent_pid>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .