今(6年後)はずっと簡単です!
Spawn はchildObjectを返します。これでイベントをリッスンできます。イベントは次のとおりです。
- クラス: ChildProcess
- イベント: 「エラー」
- イベント: 「終了」
- イベント: 「閉じる」
- イベント: 「切断」
- イベント: 「メッセージ」
childObject からのオブジェクトの束もあります。それらは次のとおりです。
- クラス: ChildProcess
- child.stdin
- child.stdout
- child.stderr
- child.stdio
- child.pid
- child.connected
- child.kill([シグナル])
- child.send(メッセージ[, sendHandle][, コールバック])
- 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);
});
}
上記の方法を使用すると、スクリプトからの出力のすべての行をクライアントに送信できます (たとえば、 でイベントを受信したときに Socket.io を使用して各行を送信しますstdout
) 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);