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
|
/**
* 根据进程名称,查询 进程是否存在
*
* @param serverName
*/
const checkProcessIsExistByName = (serverName: string): Promise<boolean> => {
return new Promise((res, rej) => {
if (!isWin()) {
rej("checkProcessIsExistByName 方法目前只能在 window 系统上使用");
}
exec(
"tasklist /fo csv",
{
maxBuffer: 1024 * 1024 * 1024,
},
function (err, stdout) {
if (err) {
rej(`error occur, cause: ${err.message}`);
return;
}
try {
stdout.split("\n").forEach((line: string) => {
if (line) {
// let [name, pid] = line.trim().split(',');
let [name] = line.trim().split(",");
name = JSON.parse(name);
// pid = JSON.parse(pid);
// console.log({ name, pid });
if (name === serverName) {
res(true);
}
}
});
res(false);
} catch (e) {
rej(
`checkProcessIsExistByName deal stdout error, cause: ${String(e)}`
);
}
}
);
});
};
|