Answer the question
In order to leave comments, you need to log in
How to pass the result to a variable?
I want to try to make primitive monitoring, standard NodeJS. Almost everything you need is in os. But you still need to use disks (how much space is occupied / free). I found code examples, but I need to transfer not to the console, but to any variable for further work. Most likely I'm stupid in the basic understanding of JS.
var child_process = require('child_process');
var MyVar;
var MyVar2;
// exec: spawns a shell.
child_process.exec('df -h', function(error, stdout, stderr){
//console.log(JSON.stringify(stdout, null, 2).split("\\n"));
MyVar = JSON.stringify(stdout, null, 2).split("\\n");
});
child_process.exec('uptime', function(error, stdout, stderr){
//console.log(JSON.stringify(stdout, null, 2));
MyVar2 = JSON.stringify(stdout, null, 2);
});
console.log(MyVar);
console.log(MyVar2);
Answer the question
In order to leave comments, you need to log in
No way, you need to use callback or promises.
These are asynchronous requests, console.log is executed before child_process.exec has time to fire.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question