现在(6年后)要容易得多!
Spawn返回childObject,然后您可以使用此监听事件。这些事件是:
- 类:ChildProcess
- 事件:“错误”
- 事件:“退出”
- 事件:“关闭”
- 事件:“断开连接”
- 事件:“消息”
还有来自childObject的一堆对象,它们是:
- 类:ChildProcess
- child.stdin
- child.stdout
- child.stderr
- 小孩
- child.pid
- 与孩子有关
- child.kill([signal])
- child.send(message [,sendHandle] [,callback])
- child.disconnect()
在此处查看有关childObject的更多信息:https ://nodejs.org/api/child_process.html
异步
如果要在节点仍然能够继续执行时在后台运行进程,请使用异步方法。您仍然可以选择在过程完成之后以及过程有任何输出时执行操作(例如,如果要将脚本的输出发送给客户端)。
child_process.spawn(...); (节点v0.1.90)
var spawn = require('child_process').spawn;
var child = spawn('node ./commands/server.js');
// You can also use a variable to save the output
// for when the script closes later
var scriptOutput = "";
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
//Here is where the output goes
console.log('stdout: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
//Here is where the error output goes
console.log('stderr: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.on('close', function(code) {
//Here you can get the exit code of the script
console.log('closing code: ' + code);
console.log('Full output of script: ',scriptOutput);
});
这是使用回调+异步方法的方式:
var child_process = require('child_process');
console.log("Node Version: ", process.version);
run_script("ls", ["-l", "/home"], function(output, exit_code) {
console.log("Process Finished.");
console.log('closing code: ' + exit_code);
console.log('Full output of script: ',output);
});
console.log ("Continuing to do node things while the process runs at the same time...");
// This function will output the lines from the script
// AS is runs, AND will return the full combined output
// as well as exit code when it's done (using the callback).
function run_script(command, args, callback) {
console.log("Starting Process.");
var child = child_process.spawn(command, args);
var scriptOutput = "";
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) {
console.log('stdout: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
data=data.toString();
scriptOutput+=data;
});
child.on('close', function(code) {
callback(scriptOutput,code);
});
}
使用上述方法,您可以将脚本的每一行输出发送到客户端(例如,当您在stdout
或上收到事件时,使用Socket.io发送每一行stderr
)。
同步
如果您希望节点停止正在执行的操作并等待脚本完成,则可以使用同步版本:
child_process.spawnSync(...);(节点v0.11.12 +)
此方法存在问题:
- 如果脚本需要一段时间才能完成,则服务器将挂起该时间!
- 仅在脚本运行完毕后才返回stdout。因为它是同步的,所以直到当前行结束它才能继续。因此,在生成行完成之前,无法捕获“ stdout”事件。
如何使用它:
var child_process = require('child_process');
var child = child_process.spawnSync("ls", ["-l", "/home"], { encoding : 'utf8' });
console.log("Process finished.");
if(child.error) {
console.log("ERROR: ",child.error);
}
console.log("stdout: ",child.stdout);
console.log("stderr: ",child.stderr);
console.log("exist code: ",child.status);
python
则不要忘记传递其-u
标志以不缓冲控制台输出,否则看起来脚本不是实时的 stackoverflow.com/a/49947671/906265