Trying to run fping from nodejs via child_process.spawn() is and capturing output via cp.stderr.on('data') sometimes results in incomplete data in the buffer. Here's my sample snippet:
const ChildProcess = require('child_process');
const args = [
'-A','-c10','-b1472','-B1',
'-r0','-O0','-q', '-p100',
'google.com', 'slack.com', 'github.com'
];
function runChildProcess() {
const child = ChildProcess.spawn('fping', args);
child.on('exit', (code) =>
console.log('Process exited with code', code)
);
child.stdout.on('data', (data) => {
console.log('stdout:', data.toString().length, 'chars');
});
child.stderr.on('data', (data) => {
console.log('stderr:', data.toString().length, 'chars');
});
}
setInterval(runChildProcess, 1500);
This produces output like so:
stderr: 219 chars
Process exited with code 0
stderr: 208 chars
stderr: 11 chars
Process exited with code 0
stderr: 218 chars
stderr: 1 chars
Process exited with code 0
stderr: 219 chars
Process exited with code 0
stderr: 219 chars
Process exited with code 0
stderr: 219 chars
Process exited with code 0
stderr: 218 chars
stderr: 1 chars
Process exited with code 0
As you can see, sometimes it invokes the callback two times, sometimes one, I had times where it was invoked 3 or 4 times with small reads. Is there a way to ensure all available data is read and not just some of it?