1

我想获得免费的 -m(linux shell 命令)并使用下面的源代码将其结果存储到变量中:

 var spawn = require('child_process').spawn,
     command  = spawn('free', ['-m']);
 command.stdout.pipe(process.stdout);

有什么方法可以将 process.stdout 存储在变量中,请给我一些建议

4

1 回答 1

1

这很简单child_process.exec

var child = require("child_process");
var freeOut;
child.exec("free", ["-m"], function (error, stdout, stderr) {
  if (error) {
    console.error(error, stderr);
    return;
  }
  //stdout and stderr are available here
  freeOut = stdout;
  process.stdout.write(stdout);
});
//Note. Do NOT use freeOut here. exec is async. Can only use in the callback
于 2014-11-08T06:56:41.953 回答