get execFile stdOut on chunks

限于喜欢 提交于 2020-02-04 08:40:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!