我正在使用,node.js v0.6.10
虽然我在0.6.7
. 基本上,我运行一个子进程,使用spawn
它启动另一个 node.js 进程,并通过以下方式进行通信stdout
,stdin
这是两个脚本:
父 ( cli.js
):
var spawn = require("child_process").spawn;
var doSpawn = function(callback){
var child = spawn('child.js');
child.on('exit', function(code){
console.log("Child exited with code " + code);
});
child.stdin.write("ping");
child.stdin.end();
};
doSpawn();
setTimeout(function(){}, 10000);
child.js
var run = function(){
process.stdout.on('drain', function(){
process.exit(0);
});
process.stdout.write(stdout);
};
var stdin = process.stdin;
stdin.resume();
stdin.setEncoding("utf8");
var stdout = '';
stdin.on('data', function(data){
stdout += data;
});
stdin.on('end', run);
然后当我运行时node cli.js
:
$ node cli.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: write EPIPE
at errnoException (net.js:642:11)
at Object.afterWrite [as oncomplete] (net.js:480:18)