I spawn a shell to read from input then execute
const { spawn } = require('child_process')
const child = spawn('while :;do read a;$a;done', [], { shell: true,stdio:'inherit' })
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`)
})
with stdio:'inherit'
, it will consume 100% cpu. But if I remove stdio:'inherit'
and add process.stdin.pipe(child.stdin)
, it works perfectly
the doc says
'inherit' - equivalent to [process.stdin, process.stdout, process.stderr]
So what's the actual meaning of inherit
and the difference between them
Little late to answer sorry, but for anybody else who is looking... I was confused about this too at first and the documentation is a bit light.
inherit
means the stdios (stdin, stdout, stderr) for the parent process will be handed off to the child process. So if the stdin on child process is set to inherit
, any key presses will target the child process and not the parent.
pipe
means that the stdios for both the parent and child are mapped together. However accessing piped data through those pips is accessed through events. So if you spawn a child process with a stdout of pipe
, you can access any output of that process through the use of childProcess.stdout.on('data', callbakFn)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With