-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
50 lines (42 loc) · 1.98 KB
/
node_helper.js
File metadata and controls
50 lines (42 loc) · 1.98 KB
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
const NodeHelper = require("node_helper");
const { exec } = require("child_process");
module.exports = NodeHelper.create({
start() {
console.log("MMM-ProcessStatus helper started");
},
socketNotificationReceived(notification, payload) {
if (notification === "CHECK_PROCESS") {
let results = [];
let completedCount = 0;
const totalProcesses = payload.processNames.length;
payload.processNames.forEach((processName) => {
const command = payload.checkCommand || `pgrep -x "${processName}"`;
exec(command, (error, stdout, stderr) => {
let status = "stopped";
let errorMsg = null;
// Logic Fix:
// pgrep returns exit code 1 if process is NOT found. This is NOT an error.
// We only treat it as an error if the exit code is > 1 OR if stderr indicates a real issue.
if (error && error.code !== 1) {
// Real error (syntax, permission, etc.)
errorMsg = error.message;
console.warn(`[MMM-ProcessStatus] Real error checking ${processName}: ${errorMsg}`);
} else if (stdout.trim() !== "") {
// Process found (stdout has PIDs)
status = "running";
}
// If error.code === 1, it means "not found", so we leave status as "stopped"
results.push({
processName,
status,
error: errorMsg // Optional: pass error info if needed
});
completedCount++;
if (completedCount === totalProcesses) {
this.sendSocketNotification("PROCESS_STATUS", results);
}
});
});
}
}
});