What is tree-kill?
The tree-kill npm package is used to kill all processes in the process tree, including the root process. It is particularly useful when you need to clean up all child processes spawned by a parent process, ensuring that no orphan processes remain running.
What are tree-kill's main functionalities?
Kill process trees on different platforms
This code kills the process tree with the PID 12345 on both Windows and Unix systems.
const treeKill = require('tree-kill');
treeKill(12345);
Kill process trees with a specific signal
This code sends the SIGKILL signal to the process tree with the PID 12345, forcing it to terminate immediately.
const treeKill = require('tree-kill');
treeKill(12345, 'SIGKILL');
Kill process trees with a callback
This code attempts to terminate the process tree with the PID 12345 using the SIGTERM signal and provides a callback to handle the result.
const treeKill = require('tree-kill');
treeKill(12345, 'SIGTERM', function(err) {
if (err) {
console.error('Error:', err);
} else {
console.log('Process tree terminated successfully.');
}
});
Other packages similar to tree-kill
ps-tree
The ps-tree package is similar to tree-kill in that it allows you to list all child processes of a given PID. However, it does not provide a built-in method to kill these processes; you would need to implement that functionality yourself.
fkill
fkill is a more feature-rich process killer that can kill processes by name, port, and more. It also supports killing processes across platforms but does not specifically target the entire process tree like tree-kill does.
pidtree
pidtree is another package that can list child processes of a given PID. Similar to ps-tree, it does not have the capability to kill the processes, which is the main feature of tree-kill.
Tree Kill
Kill all processes in the process tree, including the root process.
Example
Kill all the children processes of the process with pid 1
, including the process with pid 1
itself:
var kill = require('tree-kill');
kill(1, 'SIGKILL');
Supports callbacks with error handling:
var kill = require('tree-kill');
kill(1, 'SIGKILL', function(err) {
});
Methods
require('tree-kill')(pid, [signal], [callback]);
Sends signal signal
to all children processes of the process with pid pid
, including pid
. Signal defaults to SIGTERM
.
For Linux, this uses ps -o pid --no-headers --ppid PID
to find the parent pids of PID
.
For Windows, this uses 'taskkill /pid PID /T /F'
to kill the process tree.
Install
With npm do:
npm install tree-kill
License
MIT