Node.js spawning parent process and capturing subprocess output

Viewed 23
const options = {
  cwd: process.cwd(),
  shell: true,
  stdio: 'inherit',
}

const firstCommand = 'pktmon stop'
const secondCommand = 'pktmon filter remove'
const thirdCommand = 'pktmon filter add --ethertype 0x88cc'
const foruthCommand = 'pktmon start --etw --pkt-size 0 --log-mode real-time --comp nics'

const child = require('child_process').spawn(`${firstCommand} && ${secondCommand} && 
${thirdCommand} && ${foruthCommand}`,[], options)

//none of child outputs are triggered by subprocess output
child.stdout.on('data', function(data){
  console.log('stdout', data.toString())
})
child.stderr.on('data', function (data) {
  console.log('stderr',data.toString())
})
child.on('message', (data) => {
  console.log('message',data)
})

Subprocess output is showed on the terminal but how can i actually capture them and save output to a variable?

the reason im using stdio: 'inherit' is because - when i'm not using it the terminal is freezing when last command being executed..

0 Answers
Related