NodeJS exec/spawn stdout cuts off the stream at 8192 characters
try this :
const { spawn } = require('child_process');const cmd = spawn('command', ['arg1', 'arg2']);let bufferArray= []/*cmd.stdout.setEncoding('utf8'); sets encdoing defualt encoding is buffer*/cmd.stdout.on('data', (data) => { console.log(`stdout: ${data}`); bufferArray.push(data)});cmd.stderr.on('data', (data) => { console.error(`stderr: ${data}`);});cmd.on('close', (code) => { console.log(`child process exited with code ${code}`); let dataBuffer = Buffer.concate(bufferArray]; console.log(dataBuffer.toString())});
this could be useful: Node.js spawn child process and get terminal output live
Turns out that the shell command had a process.exit()
statement that is being called before the stdout buffer is fully flushed.
So stdout will send 8192 chars and since it's asynchronous the process will go on to the next statement, one of them being process.exit() and it will kill the process before flushing out the rest of the stdout buffer.
TL;DR - exec/spawn works correctly, shell command exits before stdout fully flushed
The default buffer size for child_process.exec
is 1MB, so try not passing a maxBuffer
; however, it would be much better to use child_process.spawn
so that you get the output as a stream.