about summary refs log tree commit diff
path: root/editors/code/src/utils
diff options
context:
space:
mode:
authorEdwin Cheng <edwin0cheng@gmail.com>2019-04-02 01:11:22 +0800
committerEdwin Cheng <edwin0cheng@gmail.com>2019-04-02 15:03:31 +0800
commitb84d0fc1a307f6103ea2c2620a106db821696434 (patch)
tree99d158e992a58a07cbf51676add9784583b401c8 /editors/code/src/utils
parentc894a3e19b0e658622a03a7d0539a78a433b42ae (diff)
downloadrust-b84d0fc1a307f6103ea2c2620a106db821696434.tar.gz
rust-b84d0fc1a307f6103ea2c2620a106db821696434.zip
Add proper process teminate method
Diffstat (limited to 'editors/code/src/utils')
-rw-r--r--editors/code/src/utils/processes.ts40
-rw-r--r--editors/code/src/utils/terminateProcess.sh12
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 00000000000..09fdf6e249a
--- /dev/null
+++ b/editors/code/src/utils/processes.ts
@@ -0,0 +1,40 @@
+'use strict';
+
+import * as cp from 'child_process';
+import ChildProcess = cp.ChildProcess;
+
+import { join } from 'path';
+
+const isWindows = (process.platform === 'win32');
+const isMacintosh = (process.platform === 'darwin');
+const isLinux = (process.platform === 'linux');
+export function terminate(process: ChildProcess, cwd?: string): boolean {
+	if (isWindows) {
+		try {
+			// This we run in Atom execFileSync is available.
+			// Ignore stderr since this is otherwise piped to parent.stderr
+			// which might be already closed.
+			const options: any = {
+				stdio: ['pipe', 'pipe', 'ignore']
+			};
+			if (cwd) {
+				options.cwd = cwd
+			}
+			(cp).execFileSync('taskkill', ['/T', '/F', '/PID', process.pid.toString()], options);
+			return true;
+		} catch (err) {
+			return false;
+		}
+	} else if (isLinux || isMacintosh) {
+		try {
+			const cmd = join(__dirname, 'terminateProcess.sh');
+			const result = cp.spawnSync(cmd, [process.pid.toString()]);
+			return result.error ? false : true;
+		} catch (err) {
+			return false;
+		}
+	} else {
+		process.kill('SIGKILL');
+		return true;
+	}
+}
\ 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 00000000000..2ec9e1c2ec3
--- /dev/null
+++ b/editors/code/src/utils/terminateProcess.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+terminateTree() {
+    for cpid in $(pgrep -P $1); do
+        terminateTree $cpid
+    done
+    kill -9 $1 > /dev/null 2>&1
+}
+
+for pid in $*; do
+    terminateTree $pid
+done
\ No newline at end of file