2015-01-12 36 views
1

我想使用的execfile和日誌標準輸出,讓該任務的完成百分比,但回調函數:得到的execfile標準輸出上塊

var child = require('child_process'); 

child.execFile("path/to/the/file", options, function (error, stdout, stderr) { 
    console.log('stdout: ' + stdout); 
}); 

等待,直到處理完成,然後記錄一切在一旦。 我如何獲得信息,同時正在處理中的部分記錄呢?,我也試試這個:

child.stdout.on('data', function (data) { 
    console.log(data); 
}); 

,但我得到這個錯誤:Cannot read property 'on' of undefined"

回答

0

您應該使用.spawn()代替.exec()/.execFile()用於流輸出:

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` 
}); 
+0

謝謝,這工作完美。 – Alxs24