diff options
author | Edwin Cheng <[email protected]> | 2019-04-01 18:11:22 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-04-02 08:03:31 +0100 |
commit | b84d0fc1a307f6103ea2c2620a106db821696434 (patch) | |
tree | 99d158e992a58a07cbf51676add9784583b401c8 /editors/code/src/utils | |
parent | c894a3e19b0e658622a03a7d0539a78a433b42ae (diff) |
Add proper process teminate method
Diffstat (limited to 'editors/code/src/utils')
-rw-r--r-- | editors/code/src/utils/processes.ts | 40 | ||||
-rw-r--r-- | editors/code/src/utils/terminateProcess.sh | 12 |
2 files changed, 52 insertions, 0 deletions
diff --git a/editors/code/src/utils/processes.ts b/editors/code/src/utils/processes.ts new file mode 100644 index 000000000..09fdf6e24 --- /dev/null +++ b/editors/code/src/utils/processes.ts | |||
@@ -0,0 +1,40 @@ | |||
1 | 'use strict'; | ||
2 | |||
3 | import * as cp from 'child_process'; | ||
4 | import ChildProcess = cp.ChildProcess; | ||
5 | |||
6 | import { join } from 'path'; | ||
7 | |||
8 | const isWindows = (process.platform === 'win32'); | ||
9 | const isMacintosh = (process.platform === 'darwin'); | ||
10 | const isLinux = (process.platform === 'linux'); | ||
11 | export function terminate(process: ChildProcess, cwd?: string): boolean { | ||
12 | if (isWindows) { | ||
13 | try { | ||
14 | // This we run in Atom execFileSync is available. | ||
15 | // Ignore stderr since this is otherwise piped to parent.stderr | ||
16 | // which might be already closed. | ||
17 | const options: any = { | ||
18 | stdio: ['pipe', 'pipe', 'ignore'] | ||
19 | }; | ||
20 | if (cwd) { | ||
21 | options.cwd = cwd | ||
22 | } | ||
23 | (cp).execFileSync('taskkill', ['/T', '/F', '/PID', process.pid.toString()], options); | ||
24 | return true; | ||
25 | } catch (err) { | ||
26 | return false; | ||
27 | } | ||
28 | } else if (isLinux || isMacintosh) { | ||
29 | try { | ||
30 | const cmd = join(__dirname, 'terminateProcess.sh'); | ||
31 | const result = cp.spawnSync(cmd, [process.pid.toString()]); | ||
32 | return result.error ? false : true; | ||
33 | } catch (err) { | ||
34 | return false; | ||
35 | } | ||
36 | } else { | ||
37 | process.kill('SIGKILL'); | ||
38 | return true; | ||
39 | } | ||
40 | } \ No newline at end of file | ||
diff --git a/editors/code/src/utils/terminateProcess.sh b/editors/code/src/utils/terminateProcess.sh new file mode 100644 index 000000000..2ec9e1c2e --- /dev/null +++ b/editors/code/src/utils/terminateProcess.sh | |||
@@ -0,0 +1,12 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | terminateTree() { | ||
4 | for cpid in $(pgrep -P $1); do | ||
5 | terminateTree $cpid | ||
6 | done | ||
7 | kill -9 $1 > /dev/null 2>&1 | ||
8 | } | ||
9 | |||
10 | for pid in $*; do | ||
11 | terminateTree $pid | ||
12 | done \ No newline at end of file | ||