Killing Parent process along with child process using SIGKILL

Asked
Active3 hr before
Viewed126 times

7 Answers

usingprocesschildparent
90%

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:,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.,You have to send the signal to the process group if you want all processes for a given group to receive the signal.,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
load more v
88%

By default killing a parent process does not kill the children processes. ,Thanks for contributing an answer to Unix & Linux Stack Exchange!,I suggest you look for other questions about how to kill both the parent and child using the process group (a negative PID).,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:

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
}
72%

killpg() — Send a signal to a process group,If pid is greater than 0, kill() sends its signal to the process whose ID is equal to pid.,pthread_kill() — Send a signal to a thread,kill() returns 0 if it has permission to send sig to any of the processes specified by pid.

Format

#define _POSIX_SOURCE
#include <signal.h>

   int kill(pid_t pid, int sig);
load more v
65%

setpgid() for child to join parent's process group, called in child process after fork() before exec(),polling for getppid() == 1 on child, then exit

       PR_SET_PDEATHSIG(since Linux 2.1 .57)
       Set the parent process death signal of the calling process to arg2(either a signal value in the range 1..maxsig, or 0 to clear).This is the signal that the calling process will get when its parent dies.
       This value is cleared
       for the child of a fork(2) and(since Linux 2.4 .36 / 2.6 .23) when executing a
       set - user - ID or set - group - ID binary.This value is preserved across execve(2).

       PR_GET_PDEATHSIG(since Linux 2.3 .15)
       Return the current value of the parent process death signal, in the location pointed to by(int * )
       arg2.
load more v
75%

SIGKILL (9) – Kill signal. Use SIGKILL as a last resort to kill process. It will not save data or cleaning kill the process.,how to kill a process using system call?,If it still didn’t die, type: $ pstree Now find process’ parent process (the one to the left of it), and repeat everything for the parent process.,“grep” doesn’t kill a process, it is only used to filter output of other commands such as “ps” or “pstree”, at least in this context.

pidof httpd
pidof apache2
pidof firefox
load more v
40%

Sometimes, sending a SIGTERM to a process will cause it to send SIGTERM to all its child processes. However, sometimes this doesn't work.,Is there a command or a utility that will allow me to kill a process and all its child processes at the same time? I usually resort to manually collecting all the pids into one kill command, but it feels stupid.,In that case, even the SIGKILL will end up doing nothing to the process; the child process will probably in a similar state of retardation.,So you then proceed to send a SIGKILL, which tells the system itself to Make That Process Stop, Now - which it can do most of the times, except when the process is in a state of ininterruptible sleep, like waiting for some I/O input or similar.

Assuming that the processes share a session identifier (which they should unless they've explicitly called setsid(), you can kill them by session using pkill:

user @machine: ~ > ps - o pid, sess, cmd - U user
PID SESS CMD
12804 12804 - bash
12916 12804 ps - o pid, sess, cmd - U user
user @machine: ~ > sh
sh - 3.00 $ sh
sh - 3.00 $ sh
sh - 3.00 $ sh
sh - 3.00 $ sh
sh - 3.00 $ sh
sh - 3.00 $ ps - o pid, sess, cmd - U user
PID SESS CMD
12804 12804 - bash
12920 12804 sh
12921 12804 sh
12922 12804 sh
12924 12804 sh
12926 12804 sh
12928 12804 sh
12937 12804 ps - o pid, sess, cmd - U user
sh - 3.00 $
load more v
22%

The pid argument can also be zero or negative to indicate that the signal should be sent to a group of processes. But, for simplicity, we will not discuss this case. , If the input is k, a SIGQUIT is sent to the other process with kill(). After this is done, this program exits. , Because of this capability, kill() can also be considered as a communication mechanism among processes with signals SIGUSR1 and SIGUSR2. , If the input is i, a SIGINT is sent to the other process with kill().

int kill(pid_t pid, int sig)
load more v

Other "using-process" queries related to "Killing Parent process along with child process using SIGKILL"