0

I'd like to have Node parse output from an ongoing bash command and do things with it in parallel to the execution of the executed command.

For example,

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

const child = exec('bash server.sh',
  (error, stdout, stderr) => {
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
    if (error !== null) {
      console.log(`exec error: ${error}`);
    }
  });

where server.sh is

yell () {
  echo "bing";
  sleep 1s;
  yell;
}

yell;

The process unfortunately hangs as the execution is waiting for server.sh to finish but it never does. Is there a way to process the output of server.sh as it appears?

2
  • Seems to me you have to do something about child to make this execute in the first place. Commented Oct 28, 2019 at 14:24
  • Correct, looks like I can add an event listener like so: ``` child.stdout.on('data', (data) => { console.log(data.toString()); }); ``` Commented Oct 28, 2019 at 14:29

1 Answer 1

1

An event listener can be bound like so:

child.stdout.on('data', (data) => {
  console.log(data.toString());
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.