问题
I am trying to use execFile and log the stdOut that gives the percentage done on the task, but the callback function:
var child = require('child_process');
child.execFile("path/to/the/file", options, function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
});
Waits until the process is done and then logs everything at once. How do i get the info while is being processed and log it in parts?, I have try this:
child.stdout.on('data', function (data) {
console.log(data);
});
But i get this error: Cannot read property 'on' of undefined"
回答1:
You should use .spawn() instead of .exec()
/.execFile()
for streaming the output:
var spawn = require('child_process').spawn;
var child = spawn("path/to/the/file", args);
child.stdout.on('data', function(data) {
console.log(data.toString());
});
child.on('close', function(code, signal) {
// process exited and no more data available on `stdout`/`stderr`
});
来源:https://stackoverflow.com/questions/27893565/get-execfile-stdout-on-chunks