pubo-node 1.0.182 → 1.0.184
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/child-process/index.js +4 -1
- package/lib/child-process/linux.d.ts +14 -0
- package/lib/child-process/linux.js +117 -0
- package/lib/child-process/p-process-type.d.ts +24 -0
- package/lib/child-process/p-process-type.js +2 -0
- package/lib/child-process/p-process.d.ts +3 -0
- package/lib/child-process/p-process.js +13 -0
- package/lib/child-process/win32.d.ts +13 -0
- package/lib/child-process/win32.js +114 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/package.json +3 -3
|
@@ -25,6 +25,9 @@ function getProcessName(pid) {
|
|
|
25
25
|
if (match && match[1]) {
|
|
26
26
|
resolve(match[1]);
|
|
27
27
|
}
|
|
28
|
+
else {
|
|
29
|
+
reject('process not found');
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
});
|
|
30
33
|
});
|
|
@@ -249,7 +252,7 @@ const getAudioCards = (filter = '') => {
|
|
|
249
252
|
});
|
|
250
253
|
};
|
|
251
254
|
exports.getAudioCards = getAudioCards;
|
|
252
|
-
const dic = ['fileSystem', 'size', 'used', 'avail', '
|
|
255
|
+
const dic = ['fileSystem', 'size', 'used', 'avail', 'usedPercent', 'mounted'];
|
|
253
256
|
const parser = (str) => {
|
|
254
257
|
return str
|
|
255
258
|
.split('\n')
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PProcess } from './p-process-type';
|
|
2
|
+
export declare class PProcessLinux implements PProcess {
|
|
3
|
+
private _SIGKILL;
|
|
4
|
+
getProcessName(pid: any): Promise<string>;
|
|
5
|
+
getPidByPort(port: any): Promise<number>;
|
|
6
|
+
getProcessCpuUseByPid(pid: number): Promise<number>;
|
|
7
|
+
getProcessCommandByPid(pid: number): Promise<string>;
|
|
8
|
+
isProcessDied(pid: number): Promise<boolean>;
|
|
9
|
+
getProcessByPpid(pid: number): Promise<number[]>;
|
|
10
|
+
SIGKILL(pid: number, signal?: number, times?: number): Promise<{
|
|
11
|
+
success: boolean;
|
|
12
|
+
error: string;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PProcessLinux = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const pubo_utils_1 = require("pubo-utils");
|
|
6
|
+
const _1 = require(".");
|
|
7
|
+
class PProcessLinux {
|
|
8
|
+
async _SIGKILL(pid, signal = 2, times = 1) {
|
|
9
|
+
if (times > 5) {
|
|
10
|
+
throw new Error('SIGKILL 失败. times > 5');
|
|
11
|
+
}
|
|
12
|
+
(0, child_process_1.exec)(`kill -${signal} ${pid}`);
|
|
13
|
+
try {
|
|
14
|
+
await (0, pubo_utils_1.waitFor)(async () => this.isProcessDied(pid), {
|
|
15
|
+
checkTime: 100,
|
|
16
|
+
timeout: 4000,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
await this._SIGKILL(pid, 9, times + 1);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
getProcessName(pid) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
(0, child_process_1.exec)(`grep "Name:" /proc/${pid}/status`, (err, data) => {
|
|
26
|
+
if (err) {
|
|
27
|
+
reject(err);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
resolve(data.toString().split(':')[1]?.trim());
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
getPidByPort(port) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
(0, child_process_1.exec)(`lsof -i:${port} | awk '{print $2}'`, (err, stdout) => {
|
|
38
|
+
if (err) {
|
|
39
|
+
reject(err);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
let res = stdout.split('\n')[1];
|
|
43
|
+
if (res) {
|
|
44
|
+
res = res.trim();
|
|
45
|
+
}
|
|
46
|
+
res = parseInt(res);
|
|
47
|
+
if (isNaN(res)) {
|
|
48
|
+
reject('process not found');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
resolve(res);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
getProcessCpuUseByPid(pid) {
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
(0, child_process_1.exec)(`ps -p ${pid} -o %cpu=`, (err, stdout) => {
|
|
59
|
+
if (err) {
|
|
60
|
+
resolve(-1);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
resolve(parseFloat(stdout.toString()));
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
getProcessCommandByPid(pid) {
|
|
69
|
+
return new Promise((resolve) => {
|
|
70
|
+
(0, child_process_1.exec)(`ps -p ${pid} -o command=`, (err, stdout) => {
|
|
71
|
+
if (err) {
|
|
72
|
+
resolve('');
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
resolve(stdout.toString().split('\n')[0]);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async isProcessDied(pid) {
|
|
81
|
+
const used = await this.getProcessCpuUseByPid(pid);
|
|
82
|
+
return used < 0;
|
|
83
|
+
}
|
|
84
|
+
getProcessByPpid(pid) {
|
|
85
|
+
return new Promise((resolve) => {
|
|
86
|
+
let child = (0, child_process_1.exec)(`ps -o pid --no-headers --ppid ${pid}`, (err, stdout) => {
|
|
87
|
+
if (err) {
|
|
88
|
+
resolve([]);
|
|
89
|
+
child = null;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
resolve(stdout
|
|
93
|
+
.split('\n')
|
|
94
|
+
.filter((item) => !!item)
|
|
95
|
+
.map((item) => parseFloat(item.trim()))
|
|
96
|
+
.filter((item) => item !== child.pid));
|
|
97
|
+
child = null;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async SIGKILL(pid, signal = 2, times = 1) {
|
|
103
|
+
const tmp = await (0, _1.getProcessList)(pid);
|
|
104
|
+
const res = { success: true, error: '' };
|
|
105
|
+
for (const item of tmp) {
|
|
106
|
+
try {
|
|
107
|
+
await this._SIGKILL(item, signal, times);
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
res.error = err;
|
|
111
|
+
res.success = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return res;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.PProcessLinux = PProcessLinux;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface PidTree {
|
|
2
|
+
pid: number;
|
|
3
|
+
children: PidTree;
|
|
4
|
+
}
|
|
5
|
+
export interface DiskInfo {
|
|
6
|
+
fileSystem: string;
|
|
7
|
+
size: number;
|
|
8
|
+
used: number;
|
|
9
|
+
avail: number;
|
|
10
|
+
usedPercent: number;
|
|
11
|
+
mounted: number;
|
|
12
|
+
}
|
|
13
|
+
export interface PProcess {
|
|
14
|
+
getProcessName(pid: number): Promise<string>;
|
|
15
|
+
getPidByPort(port: number): Promise<number>;
|
|
16
|
+
getProcessCpuUseByPid(pid: number): Promise<number>;
|
|
17
|
+
getProcessCommandByPid(pid: number): Promise<string>;
|
|
18
|
+
isProcessDied(pid: number): Promise<boolean>;
|
|
19
|
+
getProcessByPpid(ppid: number): Promise<number[]>;
|
|
20
|
+
SIGKILL(pid: number): Promise<{
|
|
21
|
+
success: boolean;
|
|
22
|
+
error: string;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PProcess = void 0;
|
|
4
|
+
const linux_1 = require("./linux");
|
|
5
|
+
const win32_1 = require("./win32");
|
|
6
|
+
exports.PProcess = (() => {
|
|
7
|
+
if (process.platform === 'win32') {
|
|
8
|
+
return new win32_1.PProcessWin32();
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return new linux_1.PProcessLinux();
|
|
12
|
+
}
|
|
13
|
+
})();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { PProcess } from './p-process-type';
|
|
2
|
+
export declare class PProcessWin32 implements PProcess {
|
|
3
|
+
getProcessName(pid: any): Promise<string>;
|
|
4
|
+
getPidByPort(port: any): Promise<number>;
|
|
5
|
+
getProcessCpuUseByPid(pid: number): Promise<number>;
|
|
6
|
+
getProcessCommandByPid(pid: number): Promise<string>;
|
|
7
|
+
isProcessDied(pid: number): Promise<boolean>;
|
|
8
|
+
getProcessByPpid(ppid: number): Promise<number[]>;
|
|
9
|
+
SIGKILL(pid: number): Promise<{
|
|
10
|
+
success: boolean;
|
|
11
|
+
error: string;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PProcessWin32 = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
class PProcessWin32 {
|
|
6
|
+
getProcessName(pid) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
(0, child_process_1.exec)(`tasklist /fi "PID eq ${pid}" /fo csv /nh`, (err, stdout) => {
|
|
9
|
+
if (err) {
|
|
10
|
+
reject(err);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
// 解析CSV格式的输出
|
|
14
|
+
const match = stdout.match(/^"(.+?)"/);
|
|
15
|
+
if (match && match[1]) {
|
|
16
|
+
resolve(match[1]);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
reject('process not found');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
getPidByPort(port) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
(0, child_process_1.exec)(`netstat -ano | findstr "${port}"`, (error, stdout) => {
|
|
28
|
+
if (error) {
|
|
29
|
+
reject(error);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const arr = stdout.split('\n');
|
|
33
|
+
if (!arr[0]) {
|
|
34
|
+
reject('process not found');
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const tmp = arr[0].split(' ');
|
|
38
|
+
let res = tmp.pop();
|
|
39
|
+
res = parseInt(res);
|
|
40
|
+
if (isNaN(res)) {
|
|
41
|
+
reject('process not found');
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
resolve(res);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async getProcessCpuUseByPid(pid) {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
(0, child_process_1.exec)(`wmic path Win32_PerfFormattedData_PerfProc_Process get IDProcess,PercentProcessorTime | findstr "${pid}"`, (error, stdout) => {
|
|
51
|
+
if (error) {
|
|
52
|
+
reject(error);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const match = stdout.match(/\d+\s+(\d+)/);
|
|
56
|
+
if (!match) {
|
|
57
|
+
reject('Process not found');
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
resolve(parseInt(match[1]));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async getProcessCommandByPid(pid) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
(0, child_process_1.exec)(`wmic process where "ProcessId=${pid}" get CommandLine /value`, (error, stdout) => {
|
|
67
|
+
if (error) {
|
|
68
|
+
reject(error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const match = stdout.match(/CommandLine=(.*)/);
|
|
72
|
+
if (!match) {
|
|
73
|
+
reject('Process not found');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
resolve(match[1].trim());
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async isProcessDied(pid) {
|
|
81
|
+
const used = await this.getProcessCpuUseByPid(pid);
|
|
82
|
+
return used < 0;
|
|
83
|
+
}
|
|
84
|
+
async getProcessByPpid(ppid) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
(0, child_process_1.exec)(`wmic process where "ParentProcessId=${ppid}" get ProcessId /value`, (error, stdout) => {
|
|
87
|
+
if (error) {
|
|
88
|
+
reject(error);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const pids = [];
|
|
92
|
+
const lines = stdout.split('\n');
|
|
93
|
+
for (const line of lines) {
|
|
94
|
+
const match = line.match(/ProcessId=(\d+)/);
|
|
95
|
+
if (match) {
|
|
96
|
+
const pid = parseInt(match[1]);
|
|
97
|
+
if (!isNaN(pid)) {
|
|
98
|
+
pids.push(pid);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
resolve(pids);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async SIGKILL(pid) {
|
|
107
|
+
return new Promise((resolve) => {
|
|
108
|
+
(0, child_process_1.exec)(`taskkill /pid ${pid} /T /F`, (err) => {
|
|
109
|
+
resolve({ success: true, error: err.toString() });
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.PProcessWin32 = PProcessWin32;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.PuboFileSystem = exports.RosTopic = exports.RosTopicManager = exports.getNetworks = exports.getWifiName = exports.isPortAvailable = exports.getDiskUsage = exports.heartbeat = exports.getPidByPort = exports.getProcessList = exports.getProcessCommandByPid = exports.getProcessCpuUseByPid = exports.getProcessByPpid = exports.getProcessTree = exports.getProcessName = exports.isProcessDied = exports.SIGKILL = exports.GrpcList = exports.createRpcClient = exports.FtpClientPool = exports.FtpClient = exports.JsonStorage = void 0;
|
|
3
|
+
exports.PProcess = exports.PuboFileSystem = exports.RosTopic = exports.RosTopicManager = exports.getNetworks = exports.getWifiName = exports.isPortAvailable = exports.getDiskUsage = exports.heartbeat = exports.getPidByPort = exports.getProcessList = exports.getProcessCommandByPid = exports.getProcessCpuUseByPid = exports.getProcessByPpid = exports.getProcessTree = exports.getProcessName = exports.isProcessDied = exports.SIGKILL = exports.GrpcList = exports.createRpcClient = exports.FtpClientPool = exports.FtpClient = exports.JsonStorage = void 0;
|
|
4
4
|
var json_1 = require("./storage/json");
|
|
5
5
|
Object.defineProperty(exports, "JsonStorage", { enumerable: true, get: function () { return json_1.JsonStorage; } });
|
|
6
6
|
var ftp_client_1 = require("./ftp-client");
|
|
@@ -31,3 +31,5 @@ Object.defineProperty(exports, "RosTopicManager", { enumerable: true, get: funct
|
|
|
31
31
|
Object.defineProperty(exports, "RosTopic", { enumerable: true, get: function () { return topic_1.RosTopic; } });
|
|
32
32
|
var file_system_1 = require("./file-system");
|
|
33
33
|
Object.defineProperty(exports, "PuboFileSystem", { enumerable: true, get: function () { return file_system_1.PuboFileSystem; } });
|
|
34
|
+
var p_process_1 = require("./child-process/p-process");
|
|
35
|
+
Object.defineProperty(exports, "PProcess", { enumerable: true, get: function () { return p_process_1.PProcess; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pubo-node",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.184",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@types/node": "^17.0.25",
|
|
21
|
-
"pubo-utils": "^1.0.
|
|
21
|
+
"pubo-utils": "^1.0.183",
|
|
22
22
|
"yaml": "^2.5.1"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "4d15c3263583a7f4b3e77a31c9783c790a0b569d",
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"del": "^5.1.0",
|
|
27
27
|
"eslint": "^8.42.0",
|