我仍然对Node.js感兴趣,但是我有一些想法。首先,我相信您需要使用
execFile而不是
spawn;
execFile用于您具有脚本路径的路径,而
spawn用于执行Node.js可以针对您的系统路径解析的众所周知的命令。
1.
提供一个回调来处理缓冲的输出:
var child = require('child_process').execFile('path/to/script', [ 'arg1', 'arg2', 'arg3', ], function(err, stdout, stderr) { // Node.js will invoke this callback when process terminates. console.log(stdout); });2.将侦听器添加到子进程的stdout
流(9thport.net)
var child = require('child_process').execFile('path/to/script', [ 'arg1', 'arg2', 'arg3' ]); // use event hooks to provide a callback to execute when data are available: child.stdout.on('data', function(data) { console.log(data.toString()); });此外,似乎存在一些选项,您可以从其中将生成的进程与Node的控制终端分离,这将使其可以异步运行。我还没有测试过,但是API文档中有一些例子如下:
child = require('child_process').execFile('path/to/script', [ 'arg1', 'arg2', 'arg3', ], { // detachment and ignored stdin are the key here: detached: true, stdio: [ 'ignore', 1, 2 ]}); // and unref() somehow disentangles the child's event loop from the parent's: child.unref(); child.stdout.on('data', function(data) { console.log(data.toString()); });


