Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created December 21, 2012 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4350236 to your computer and use it in GitHub Desktop.
Save isaacs/4350236 to your computer and use it in GitHub Desktop.
// this one passes with https://github.com/isaacs/node/commit/6377c814c65bc41dcf401bb97c57a4d15ba0e68d
if (process.argv[2] === 'child')
child();
else
parent();
function parent() {
var spawn = require('child_process').spawn;
var node = process.execPath;
var child = spawn(node, [__filename, 'child']) //, {stdio:'inherit'});
console.log('pid=%d', child.pid);
child.stdout.on('data', function(c) {
console.log("stdout: " + (''+c).trim().split(/\n/).join('\nstdout: '));
});
child.stderr.on('data', function(c) {
console.log("stderr: " + (''+c).trim().split(/\n/).join('\nstderr: '));
});
child.on('exit', function() {
console.log('child exit');
});
}
function child() {
console.log('hello');
console.log(process.stdout);
}
// this one fails
var spawn = require('child_process').spawn
var env = { NODE_DEBUG: 'net' }
var c1 = spawn('node',['-e','console.log(1)']);
c1.stdout.pipe(process.stdout);
c1.stderr.pipe(process.stderr);
console.error('c1 pid =', c1.pid);
// var c2 = spawn('node',['-e','console.log(2)'], {env: env, stdio:'inherit'})
// console.error('c2 pid = %d', c2.pid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment