(pid: number, signal: NodeJS.Signals = "SIGTERM")
| 38 | * the pgrep/ps utilities are not available. |
| 39 | */ |
| 40 | export function killProcessTree(pid: number, signal: NodeJS.Signals = "SIGTERM"): void { |
| 41 | if (process.platform === "win32") return; |
| 42 | |
| 43 | const descendants = getDescendants(pid); |
| 44 | const allPids = [...descendants.reverse(), pid]; |
| 45 | |
| 46 | for (const p of allPids) { |
| 47 | try { |
| 48 | process.kill(p, signal); |
| 49 | } catch { |
| 50 | // Already exited. |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Escalate to SIGKILL after a short grace period for any survivors. |
| 55 | if (signal !== "SIGKILL") { |
| 56 | setTimeout(() => { |
| 57 | for (const p of allPids) { |
| 58 | try { |
| 59 | process.kill(p, "SIGKILL"); |
| 60 | } catch { |
| 61 | // Already exited. |
| 62 | } |
| 63 | } |
| 64 | }, 500).unref(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | function getDescendants(pid: number): number[] { |
| 69 | let children: number[]; |
no test coverage detected