MCPcopy Create free account
hub / github.com/tiann/hapi / killProcessWindows

Function killProcessWindows

cli/src/utils/process.ts:19–48  ·  view source on GitHub ↗
(pid: number, force: boolean)

Source from the content-addressed store, hash-verified

17}
18
19function killProcessWindows(pid: number, force: boolean): boolean {
20 if (!isProcessAlive(pid)) {
21 return true;
22 }
23
24 const args = ['/T', '/PID', pid.toString()];
25 if (force) {
26 args.unshift('/F');
27 }
28 try {
29 const result = spawn.sync('taskkill', args, {
30 stdio: 'pipe',
31 windowsHide: true
32 });
33 if (result.error) {
34 return false;
35 }
36
37 if (result.status === 0) {
38 return true;
39 }
40
41 // Process teardown on Windows is racy: by the time taskkill runs, the target
42 // may already be gone, which commonly surfaces as non-zero exit codes
43 // (including 128 in some shells). Treat this as success if PID is no longer alive.
44 return !isProcessAlive(pid);
45 } catch {
46 return false;
47 }
48}
49
50export async function killProcess(pid: number, force: boolean = false): Promise<boolean> {
51 if (!Number.isFinite(pid) || pid <= 0) {

Callers 1

killProcessFunction · 0.85

Calls 2

unshiftMethod · 0.80
isProcessAliveFunction · 0.70

Tested by

no test coverage detected