-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproc.ts
172 lines (137 loc) · 3.59 KB
/
proc.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Copyright 2022 Joona Piirainen
// MIT License
//
const { build, run, readAll } = Deno;
export interface Process {
command: string; // Command to run this process
ppid: number; // The parent process ID of the process
pid: number; // Process ID
stat: string; // Process status
children?: Process[];
}
export interface KillOptions {
force?: boolean;
ignoreCase?: boolean;
tree?: boolean;
}
/**
* Get the single process information.
* Requires `--allow-run` flag
* @param pid
*/
export async function get(pid: number): Promise<Process | void> {
return (await getAll()).find((v) => v.pid === pid);
}
/**
* Get process list
* Requires `--allow-run` flag
*/
export async function getAll(): Promise<Process[]> {
const commands = build.os == "windows"
? ["wmic.exe", "PROCESS", "GET", "Name,ProcessId,ParentProcessId,Status"]
: ["ps", "-A", "-o", "comm,ppid,pid,stat"];
const ps = run({
cmd: commands,
stdout: "piped",
});
const output = new TextDecoder().decode(await readAll(ps.stdout!));
const { success, code } = await ps.status();
ps.stdout?.close();
ps.close();
if (!success || code !== 0) {
throw new Error("Fail to get process.");
}
const lines = output.split("\n").filter((v: string): string => v.trim());
lines.shift();
const processList: Process[] = lines.map((line: string): Process => {
const columns = line.trim().split(/\s+/);
return {
command: columns[0],
ppid: +columns[1],
pid: +columns[2],
stat: columns[3],
};
});
return processList;
}
/**
* Get process tree
* Requires `--allow-run` flag
*/
export async function psTree(pid: number): Promise<Process[]> {
const items = await getAll();
const nest = (items: Process[], pid: number): Process[] => {
return items
.filter((item) => item.ppid === pid)
.map((item) => {
const children = nest(items, item.pid);
if (!children.length) {
return item;
} else {
return { ...item, children };
}
}) as Process[];
};
return nest(items, pid);
}
function getKillCommand(
pidOrName: number | string,
options: KillOptions = {},
): string[] {
const killByName = typeof pidOrName === "string";
if (build.os === "windows") {
const commands = ["taskkill"];
if (options.force) {
commands.push("/f");
}
if (options.tree) {
commands.push("/t");
}
commands.push(killByName ? "/im" : "/pid", pidOrName + "");
return commands;
} else if (build.os === "linux") {
const commands = [killByName ? "killall" : "kill"];
if (options.force) {
commands.push("-9");
}
if (killByName && options.ignoreCase) {
commands.push("-I");
}
commands.push(pidOrName + "");
return commands;
} else {
const commands = [killByName ? "pkill" : "kill"];
if (options.force) {
commands.push("-9");
}
if (killByName && options.ignoreCase) {
commands.push("-i");
}
commands.push(pidOrName + "");
return commands;
}
}
/**
* kill process
* Requires `--allow-run` flag
* @param pidOrName pid or process name
* @param options
*/
export async function kill(
pidOrName: number | string,
options: KillOptions = {},
): Promise<void> {
const commands = getKillCommand(pidOrName, options);
const ps = run({
cmd: commands,
stderr: "piped",
});
const { success, code } = await ps.status();
ps.stderr?.close();
ps.close();
if (!success || code !== 0) {
const msg = new TextDecoder().decode(await readAll(ps.stderr!));
throw new Error(msg || "exit with code: " + code);
}
}