0
votes

I have been trying to call an custom bash script using child_process spawn module from my node application and I want to capture the stdout echoed by the shell script. I have tried listening to 'data' event but nothing helps. Here is the code:enter code here

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

const subprocess = spawn('.\\start_recording.sh', [], {shell:true});

subprocess.stdout.on('data', (data) => {
  data = data.toString();
  console.log(`Received chunk ${data}`);
});

I even tried using:

subprocess.stderr.pipe(process.stderr, {end: false});
subprocess.stdout.pipe(process.stdout, {end: false});

Still 'data' event not get listened.

Here is my bash script start_recording.sh:

#!/bin/bash
echo something
1

1 Answers

0
votes

according to nodejs documentation, child_process is asynchronous so try to wrap it in an async function . https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

try this:

async ()=>{
    let spawExample = await subprocess.stdout.on('data', (data) => {
      data = data.toString();
      console.log(`Received chunk ${data}`);
      return data
    });

}