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)
- 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).
- 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
Initial State of Program Registers and Stack on Linux Arm
Determining When Nasm Can Infer the Size of the Mov Operation
Getting Pid and Details for Topmost Window
Don't Add "+" to Linux Kernel Version
How to Print to the Console in Color in a Cross-Platform Manner
Single File Volume Mounted as Directory in Docker
How to Check If a Program Is Run in Bash on Ubuntu on Windows and Not Just Plain Ubuntu
What Do the Numbers in /Proc/Loadavg Mean on Linux
How to Upgrade Aws Cli to the Latest Version
Uevent Sent from Kernel to User Space (Udev)
What Is the Equivalent of Solution Files in Linux C++ Ide
Http Post and Get Using Curl in Linux
Difference Between Arm-Eabi Arm-Gnueabi and Gnueabi-Hf Compilers
How to Install PHP 7 on Ec2 T2.Micro Instance Running Amazon Linux Distro
Tar Archiving That Takes Input from a List of Files