Home > Net >  kill -s SIGTERM kills parent process and one level child process only
kill -s SIGTERM kills parent process and one level child process only

Time:12-13

I've been doing some experimenting with writing a command to kill parent and all it's children recursively. I've a script as below

parent.sh:

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

child.sh:

#!/bin/bash
sleep infinity

Started command using

su oracle -c parent.sh &

I see a process tree like below

[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

When I send sigterm to 14129 using kill -s SIGTERM 14129 it appears to kill 14129 and then 14130 goes down as well immediately; but 14131 stays up for a very long time. The last level child appears to have been reparented and has become a zombie.

oracle   14131     1  0 12:39 ?        00:00:00 /bin/bash /home/oracle/child.sh
  1. If kill doesn't terminate any child processes why did 14130 get killed when I sent a SIGTERM to 14129?
  2. If kill can kill child processes, why would does it go only one level down? Is the behavior here guaranteed?

CodePudding user response:

The relevant part of what pilcrow provided is this:

SIGNALS top

   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].

That harsher method, SIGKILL, prevents that child process from attempting to kill its own children, hence the zombie state.

I haven't used it myself, but it seems that something like

killall --process-group parent.sh

would kill all processes tied to the process group associated with the "parent.sh" script. BUT ... not sure if "--wait" will serve you well, if the method used in the attempt to terminate is not being accepted.

  • Related