* Recursively collects all descendant PIDs of a process (depth-first). * Returns PIDs in child-first order (leaves first, root last).
(pid: number)
| 70 | * Returns PIDs in child-first order (leaves first, root last). |
| 71 | */ |
| 72 | function 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. |