Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
The ps-tree npm package is a utility for listing all processes spawned by a given process, including their pid, command, arguments, and environment. It is particularly useful for managing process trees in Node.js applications, allowing developers to monitor or terminate groups of related processes efficiently.
Listing child processes
This feature allows you to list all child processes spawned by a specific process. The callback function receives an array of child processes, each represented as an object containing details such as pid, command, and arguments.
const psTree = require('ps-tree');
const pid = 1234; // Example PID of the parent process
psTree(pid, function (err, children) {
console.log(children);
});
The tree-kill package provides functionality to kill all processes in the process tree, including the root process. It is similar to ps-tree in that it deals with process trees, but its primary focus is on terminating processes rather than listing them.
Pidtree is another alternative that, like ps-tree, allows for listing child processes of a given PID. However, pidtree offers a promise-based API and might provide a more modern interface for handling asynchronous operations compared to ps-tree's callback-based approach.
Sometimes you cannot kill child processes like you would expect, this a feature of UNIX.
in UNIX, a process may terminate by using the exit call, and it's parent process may wait for that event by using the wait system call. the wait system call returns the process identifier of a terminated child, so that the parent tell which of the possibly many children has terminated. If the parent terminates, however, all it's children have assigned as their new parent the init process. Thus, the children still have a parent to collect their status and execution statistics. (from "operating system concepts")
Solution: use ps-tree
to get all processes that a child_process
may have started, so that they may all be terminated.
var cp = require('child_process'),
psTree = require('ps-tree');
var child = cp.exec("node -e 'while (true);'", function () {...});
// This will not actually kill the child it will kill the `sh` process.
child.kill();
wtf? it's because exec actually works like this:
function exec (cmd, cb) {
spawn('sh', ['-c', cmd]);
...
}
sh
starts parses the command string and starts processes, and waits for them to terminate, but exec
returns a process object with the pid of the sh
process.
However, since it is in wait
mode killing it does not kill the children.
Use ps-tree
like this:
var cp = require('child_process'),
psTree = require('ps-tree');
var child = cp.exec("node -e 'while (true);'", function () { /*...*/ });
psTree(child.pid, function (err, children) {
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })));
});
If you prefer to run psTree from the command line, use: node ./bin/ps-tree.js
The ps-tree
module behaves differently on *nix vs. Windows by spawning different programs and parsing their output. This is based on process.platform
and not on checking to see if a ps
compatible program exists on the system.
$ ps -A -o comm,ppid,pid,stat
COMMAND PPID PID STAT
bbsd 2899 16958 Ss
watch <defunct> 1914 16964 Z
ps 20688 16965 R+
wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)
> wmic PROCESS GET Name,ProcessId,ParentProcessId,Status
Name ParentProcessId ProcessId Status
System Idle Process 0 0
System 0 4
smss.exe 4 228
1.2.0
FAQs
Get all children of a pid
The npm package ps-tree receives a total of 1,911,040 weekly downloads. As such, ps-tree popularity was classified as popular.
We found that ps-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.