|
| 1 | +// Adapted from https://raw.githubusercontent.com/pkrumins/node-tree-kill/deee138/index.jss |
| 2 | +import { spawn, exec, ExecException, ChildProcess } from 'child_process'; |
| 3 | + |
| 4 | +type ProcessTree = Record<number, number[]>; |
| 5 | +type ProcessMap = Record<number, number>; |
| 6 | +type SpawnFunction = (parentPid: number) => ChildProcess; |
| 7 | +type CallbackFunction = (error?: ExecException | null) => void; |
| 8 | + |
| 9 | +export async function killTree( |
| 10 | + pid: number, |
| 11 | + signal: NodeJS.Signals = 'SIGTERM' |
| 12 | +): Promise<void> { |
| 13 | + const tree: ProcessTree = {}; |
| 14 | + const pidsToProcess: ProcessMap = {}; |
| 15 | + tree[pid] = []; |
| 16 | + pidsToProcess[pid] = 1; |
| 17 | + |
| 18 | + return new Promise<void>((resolve, reject) => { |
| 19 | + const callback: CallbackFunction = (error) => { |
| 20 | + if (error) { |
| 21 | + reject(error); |
| 22 | + } else { |
| 23 | + resolve(); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + switch (process.platform) { |
| 28 | + case 'win32': |
| 29 | + exec( |
| 30 | + 'taskkill /pid ' + pid + ' /T /F', |
| 31 | + { |
| 32 | + windowsHide: true, |
| 33 | + }, |
| 34 | + (error) => { |
| 35 | + // Ignore Fatal errors (128) because it might be due to the process already being killed. |
| 36 | + // On Linux/Mac we can check ESRCH (no such process), but on Windows we can't. |
| 37 | + callback(error?.code !== 128 ? error : null); |
| 38 | + } |
| 39 | + ); |
| 40 | + break; |
| 41 | + case 'darwin': |
| 42 | + buildProcessTree( |
| 43 | + pid, |
| 44 | + tree, |
| 45 | + pidsToProcess, |
| 46 | + function (parentPid: number): ChildProcess { |
| 47 | + return spawn('pgrep', ['-P', `${parentPid}`], { |
| 48 | + windowsHide: false, |
| 49 | + }); |
| 50 | + }, |
| 51 | + function (): void { |
| 52 | + killAll(tree, signal, callback); |
| 53 | + } |
| 54 | + ); |
| 55 | + break; |
| 56 | + default: // Linux |
| 57 | + buildProcessTree( |
| 58 | + pid, |
| 59 | + tree, |
| 60 | + pidsToProcess, |
| 61 | + function (parentPid: number): ChildProcess { |
| 62 | + return spawn( |
| 63 | + 'ps', |
| 64 | + ['-o', 'pid', '--no-headers', '--ppid', `${parentPid}`], |
| 65 | + { |
| 66 | + windowsHide: false, |
| 67 | + } |
| 68 | + ); |
| 69 | + }, |
| 70 | + function (): void { |
| 71 | + killAll(tree, signal, callback); |
| 72 | + } |
| 73 | + ); |
| 74 | + break; |
| 75 | + } |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +function killAll( |
| 80 | + tree: ProcessTree, |
| 81 | + signal: NodeJS.Signals, |
| 82 | + callback?: CallbackFunction |
| 83 | +): void { |
| 84 | + const killed: ProcessMap = {}; |
| 85 | + try { |
| 86 | + Object.keys(tree).forEach(function (pid) { |
| 87 | + tree[parseInt(pid, 10)].forEach(function (pidpid) { |
| 88 | + if (!killed[pidpid]) { |
| 89 | + killPid(pidpid, signal); |
| 90 | + killed[pidpid] = 1; |
| 91 | + } |
| 92 | + }); |
| 93 | + if (!killed[parseInt(pid, 10)]) { |
| 94 | + killPid(parseInt(pid, 10), signal); |
| 95 | + killed[parseInt(pid, 10)] = 1; |
| 96 | + } |
| 97 | + }); |
| 98 | + } catch (err) { |
| 99 | + if (callback) { |
| 100 | + return callback(err as ExecException); |
| 101 | + } else { |
| 102 | + throw err; |
| 103 | + } |
| 104 | + } |
| 105 | + if (callback) { |
| 106 | + return callback(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +function killPid(pid: number, signal: NodeJS.Signals): void { |
| 111 | + try { |
| 112 | + process.kill(pid, signal); |
| 113 | + } catch (err) { |
| 114 | + const error = err as NodeJS.ErrnoException; |
| 115 | + if (error.code !== 'ESRCH') throw err; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function buildProcessTree( |
| 120 | + parentPid: number, |
| 121 | + tree: ProcessTree, |
| 122 | + pidsToProcess: ProcessMap, |
| 123 | + spawnChildProcessesList: SpawnFunction, |
| 124 | + cb: () => void |
| 125 | +): void { |
| 126 | + const ps = spawnChildProcessesList(parentPid); |
| 127 | + let allData = ''; |
| 128 | + ps.stdout?.on('data', (data: Buffer | string) => { |
| 129 | + const strData = data.toString('ascii'); |
| 130 | + allData += strData; |
| 131 | + }); |
| 132 | + |
| 133 | + const onClose = function (code: number): void { |
| 134 | + delete pidsToProcess[parentPid]; |
| 135 | + |
| 136 | + if (code != 0) { |
| 137 | + // no more parent processes |
| 138 | + if (Object.keys(pidsToProcess).length == 0) { |
| 139 | + cb(); |
| 140 | + } |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const pids = allData.match(/\d+/g); |
| 145 | + if (pids) { |
| 146 | + pids.forEach((_pid) => { |
| 147 | + const pid = parseInt(_pid, 10); |
| 148 | + tree[parentPid].push(pid); |
| 149 | + tree[pid] = []; |
| 150 | + pidsToProcess[pid] = 1; |
| 151 | + buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb); |
| 152 | + }); |
| 153 | + } |
| 154 | + }; |
| 155 | + |
| 156 | + ps.on('close', onClose); |
| 157 | +} |
0 commit comments