Why Child Process Still Alive After Parent Process Was Killed in Linux

Why child process still alive after parent process was killed in Linux?

No, 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

For example, if your parent process id has the code 1234, you will have to specify the parentpid adding the symbol minus followed by your parent process id:

kill -9 -1234

Otherwise, orphans will be linked to init, as shown by your third screenshot (PPID of the child has become 1).

How to make child process die after parent exits?

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);

See man 2 prctl for details.

Edit: This is Linux-only

How to keep child process active when parent is killed/finished (in windows)


  1. In Windows, when you kill the parent process, its children are also killed. (Not like Linux, where after the parent is killed, children get their new parent -INIT with PID 1).
  2. In windows, the parent will also not exit automatically (parent PID will be present) if the child is still running. (For your question).

So, the solution would be that you add a step where before ending of the main script, child PID is killed; and so as when child will be killed, the parent would also be able to exit successfully.

So, now if your main script finishes before the threshold time, it means no action is required by the child (And also, child will be killed before the main script completes). And if the main script crosses the threshold, the child will send a mail. And when the main script will be about to end, child will be killed first, and the main script can get exited successfully.

Node.JS child processes being killed when parent dies

you need to set the detached option

If the detached option is set, the child process will be made the
leader of a new process group. This makes it possible for the child to
continue running after the parent exits.

var child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});

When a parent process is killed by kill -9 , will subprocess also be killed?

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



Related Topics



Leave a reply



Submit