MCPcopy Create free account
hub / github.com/tiann/hapi / collectProcessTree

Function collectProcessTree

cli/src/utils/process.ts:72–89  ·  view source on GitHub ↗

* Recursively collects all descendant PIDs of a process (depth-first). * Returns PIDs in child-first order (leaves first, root last).

(pid: number)

Source from the content-addressed store, hash-verified

70 * Returns PIDs in child-first order (leaves first, root last).
71 */
72function collectProcessTree(pid: number): number[] {
73 const pids: number[] = [];
74
75 try {
76 const result = spawn.sync('pgrep', ['-P', pid.toString()], { encoding: 'utf8' });
77 if (result.stdout) {
78 const childPids = result.stdout.trim().split('\n').filter(Boolean).map(Number);
79 for (const childPid of childPids) {
80 pids.push(...collectProcessTree(childPid));
81 }
82 }
83 } catch {
84 // pgrep may not be available
85 }
86
87 pids.push(pid);
88 return pids;
89}
90
91/**
92 * Kills a process and all its descendants.

Callers 1

killProcessTreeFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected