How to kill child processes that spawn their own child processes in Node.js
If a child process in Node.js spawn their own child processes, kill()
method will not kill the child process’s own child processes. For example, if I start a process that starts it’s own child processes via child_process
module, killing that child process will not make my program to quit.
The program above will not quit if my-command
spins up some more processes.
PID range hack
We can start child processes with {detached: true}
option so those processes will not be attached to main process but they will go to a new group of processes. Then using process.kill(-pid)
method on main process we can kill all processes that are in the same group of a child process with the same pid
group. In my case, I only have one processes in this group.
Please note -
before pid
. This converts a pid
to a group of pid
s for process kill()
method.