繁体   English   中英

如何在node.js上运行带有子进程的top命令,而不会从终端获取'stdout maxBuffer exceeded'

[英]How do I run the top command with a child process on node.js without getting a 'stdout maxBuffer exceeded' from the terminal

我试图创建一个从虚拟机获取指标的REST API,但我似乎无法运行我的子进程而不会超出'stdout maxBuffer'。 这是我的代码

const { exec } = require('child_process');


exec('top', (error, stdout, stderr) => {

    if (error) {

      console.error(`exec error: ${error}`);
      return;
     }

    console.log(`stdout: ${stdout}`);

    console.log(`stderr: ${stderr}`);
});

top (使用它的默认参数)将继续写入stdout - 它不只是转储一些信息并退出。

如果要使用top ,请查看手册页。 特别是-b选项:

  -b  :Batch-mode operation
        Starts top in 'Batch' mode, which could be useful for sending output from top to other programs or to a file.  In this mode, top will not accept input and runs until the  iterations  limit  you've  set
        with the '-n' command-line option or until killed.

所以尝试使用top -b -n 1top获取单个“批量”信息,然后解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM