Adam Collins
Adam Collins

Reputation: 31

Is it possible to monitor terminal for specific output?

I am using NodeJS to run webots by command line and have redirected the stdout too the node terminal. My problem is that I want to trigger an event based on a console log. I tried redirecting the stdout of the command to another file, but this didn't seem to work.

This is the console output

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:

I want to extract 'robot2'.

Upvotes: 1

Views: 242

Answers (1)

Darko Lukić
Darko Lukić

Reputation: 196

I have just tested and the following snippet works fine for me:

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()
  //   }
});

Upvotes: 1

Related Questions