【问题标题】:Is it possible to monitor terminal for specific output?是否可以监视终端的特定输出?
【发布时间】:2021-08-03 14:19:46
【问题描述】:

我正在使用 NodeJS 通过命令行运行 webbot,并将标准输出也重定向到节点终端。我的问题是我想根据控制台日志触发事件。我尝试将命令的标准输出重定向到另一个文件,但这似乎不起作用。

这是控制台输出

INFO: sumo_example_two: Starting controller: python.exe -u sumo_example_two.py
INFO: sumo_supervisor: Starting controller: python.exe -u sumo_supervisor.py
robot2
INFO: sumo_example_one: Terminating.
INFO: sumo_example_two: Terminating.
INFO: sumo_supervisor: Terminating.
stdout:

我想提取“robot2”。

【问题讨论】:

    标签: node.js terminal console stdout webots


    【解决方案1】:

    我刚刚测试过,以下 sn-p 对我来说很好:

    const { spawn } = require('child_process');
    const ls = spawn('webots', ['--stdout']);
    
    ls.stdout.on('data', (data) => {
      console.log(`stdout: ${data}`);
    
      // Process `data` as you prefer, something like
      //
      //   if (data.includes('robot2')) {
      //     something()
      //   }
    });
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 2019-08-01
      • 2020-11-06
      • 2022-06-16
      • 2012-08-30
      相关资源
      最近更新 更多