Read the statement by Michael Teeuw here.
run Linux command from a mm2 module
-
@strawberry-3-141 said in run Linux command from a mm2 module:
A good example is the pir sensor module https://github.com/paviro/MMM-PIR-Sensor/blob/master/node_helper.js#L59 In this line it executes a linux command as a child process.
I’m quoting your post of 5 months ago. This is exactly what I needed to do. I looked at the requirements and commands in the PIR-Sensor node_helper.js.#L59. With that, I added const exec = require(‘child_process’).exec; and built my function
getTerminal: function(url) { var self= this; exec("sensors").stdout.on('data', function(stdout) { var results = stdout; console.log(results); self.sendSocketNotification("TERMINAL_RESULT", results); }) },
Success!
-
@mykle1 you didn’t need to do this
var results = stdout;
Just use stdout as the variable name in the console log and sendsocketnotification
Every wasted instruction adds to the pgm size, and CPU cycles used, which could be applied somewhere else.
Also makes maintenance harder cause u might not remember why you did something.
-
Yes sir. Duly noted. And thanks for the instruction. I try to retain all the tidbits that are given but I find myself having to relearn much of what I thought I already knew. Can you say, “Alzheimers?” :-)
Thanks again.
-
I guess I celebrated a little prematurely. Yes, I can see the data in the terminal and send it to the module.js file. However, I am unable to convert it into usable data, such as an array of objects. I’ve tried all evening with no luck. The closest I’ve gotten is single object that contains all the data:
0: stdout:"nouveau-pci-0100↵Adapter: PCI adapter↵fan1: 1170 RPM↵temp1: +43.0°C (high = +95.0°C, hyst = +3.0°C)↵ (crit = +105.0°C, hyst = +5.0°C)↵ (emerg = +135.0°C, hyst = +5.0°C)↵↵coretemp-isa-0000↵Adapter: ISA adapter↵Core 0: +46.0°C (high = +83.0°C, crit = +99.0°C)↵Core 1: +41.0°C (high = +83.0°C, crit = +99.0°C)↵Core 2: +46.0°C (high = +83.0°C, crit = +99.0°C)↵Core 3: +38.0°C (high = +83.0°C, crit = +99.0°C)↵↵f71858fg-isa-0a00↵Adapter: ISA adapter↵+3.3V: +3.31 V ↵3VSB: +3.30 V ↵Vbat: +3.20 V ↵fan1: 1910 RPM↵fan2: 1069 RPM↵fan3: 0 RPM ALARM↵temp1: +34.5°C (high = +70.0°C, hyst = +60.0°C)↵temp2: +29.5°C (high = +100.0°C, hyst = +85.0°C)↵temp3: +36.4°C (high = +100.0°C, hyst = +85.0°C)↵↵"
Using this:
getTerminal: function(url) { var self= this; // exec("sensors", (err, stdout, stderr) => console.log(stdout)); exec("sensors").stdout.on('data', function(stdout) { //console.log(JsonConvert.SerializeObject(stdout)); var s = stdout; var parts = s.split(':'); var jobj = {}; for(i=0;i < parts.length;i+=2) { jobj[parts[i]]=parts[i+1]; } console.log(JSON.stringify(jobj));
I’m stuck. haha. I am grateful for any advice.
Peace!
-
@mykle1 let me see what I can do.
Will be tomorrow. -
@mykle1
You can use Regular Expression for getting matched text with pattern.var s = stdout var fan1 = new RegExp("fan1\:\s+([0-9]+ RPM)") var fan1_found = s.match(fan1) console.log(fan1_found, fan1_found[1])
Not tested in real PC, so there could be a mistake.
-
@sean
First, thank you for your input. I do appreciate it.
-
Adding your suggestion results in an uncaught exception. . . TypeError: Cannot read property ‘1’ of null
-
Using your suggestion and taking out the “[1]” results in an array with a single object
-
-
@mykle1
null means not found. Maybe my regular espression pattern was wrong. As I wrote, it was not tested.
However if you find proper expression pattern, it will return matched string. Google it about regular expression. That is a standard way to search or replace text by pattern in most of program languages. -
-