This repository has been archived by the owner on Jun 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
kill-process-id.service.js
57 lines (53 loc) · 1.85 KB
/
kill-process-id.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* eslint-disable flowtype/require-valid-file-annotation */
/**
* NOTE: This service is used both by the Electron client
* and server, so it must only contain Node-compatible (read: non-ES6)
* code. If at some future time this requires some large amount
* of ES6 code, we can make a client-side wrapper.
*/
const childProcess = require('child_process');
const os = require('os');
const psTree = require('ps-tree');
const isWin = /^win/.test(os.platform());
// Kill the process with the given pid, as well as all
// its descendants down through the entire tree.
// When using this, make sure to await the result to insure
// the process and all its descendants are killed.
const killProcessId = doomedProcessId => {
return new Promise(resolve => {
if (isWin) {
// For Windows Support
// On Windows there is only one process so no need for psTree (see below)
// We use /f for focefully terminate process because it ask for confirmation
// We use /t to kill all child processes
// More info https://ss64.com/nt/taskkill.html
resolve(
childProcess.spawnSync('taskkill', [
'/pid',
doomedProcessId,
'/f',
'/t',
])
);
} else {
// Child node processes will persist after their parent's death
// if they are not killed first, so we need to use `psTree` to build
// a tree of children and kill them that way.
psTree(doomedProcessId, (err, children) => {
if (err) {
console.error('Could not gather process children:', err);
return resolve();
}
const childrenPIDs = children.map(child => child.PID);
resolve(
childProcess.spawnSync('kill', [
'-9',
doomedProcessId,
...childrenPIDs,
])
);
});
}
});
};
module.exports = killProcessId;