Reputation: 489
I have a NodeJS program that launches multiple child processes. What I want to do is cycle through each one, displaying stdout for whichever I choose.
I've tried launching the child processes with 'pipe' and then doing
process.stdout.pipe(childproc.stdout);
But that hangs and then crashes.
I can display just fine with 'inherit' but then I can't switch between children. I then need to be able to not display anything from them, and display the program's own information.
Any idea how to redirect my stdout between a series of child processes?
Upvotes: 1
Views: 144
Reputation: 3334
Sounds like a pretty interesting problem. I have a possible solution for you based on some work that I've done recently. If you were to spawn each process and have a handler for stdout on each one, you could check a global flag that tells you which stdout to output.
var child = spawn(sExeName, aArgs);
var child2 = spawn(sExeName2, aArgs2);
var childOutput = false;
var child2Output = false
child.stdout.on('data', function (data) {
if(childOutput === true)
{ process.stdout.write(data.toString()); }
});
child.stderr.on('data', function (data) {
if(childOutput === true)
{ process.stdout.write(data.toString()); }
});
child2.stdout.on('data', function (data) {
if(child2Output === true)
{ process.stdout.write(data.toString()); }
});
child2.stderr.on('data', function (data) {
if(child2Output === true)
{ process.stdout.write(data.toString()); }
});
Now just use whatever method you like to have it set the flags childOutput and child2Output to true or false and you can turn off and on stdout and stderr being dumped to your terminal for your child processes.
Now, how to keep from having your main process stdout being put there to, I think you would have to wrap console.log in a function that makes sure you are not trying to output from a child process at the same time.
Upvotes: 1