Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

spawn() with merged stdout and stderr #5831

Closed
jclulow opened this issue Jul 10, 2013 · 6 comments
Closed

spawn() with merged stdout and stderr #5831

jclulow opened this issue Jul 10, 2013 · 6 comments

Comments

@jclulow
Copy link

jclulow commented Jul 10, 2013

It would be pretty amazeballs to have the option of spawning a subprocess with stdout and stderr merged into a single stream. The shell analogue is basically:

someprocess 2>&1

Or, in Java parlance, redirectErrorStream(true) on a ProcessBuilder.

This would hopefully take the form of something I can pass in the stdio array to child_process.spawn().

@isaacs
Copy link

isaacs commented Jul 10, 2013

From the #libuv room:

21:50 <@piscisaureus_> isaacs: yeah, I agree, been thinking about that a long
                       time ago
21:50 <@isaacs> piscisaureus_: you CAN do this today, if you open a fd to
                somewhere, and pass it in as both stdout and stderr
21:50 <@piscisaureus_> isaacs: we could do something like: stdio: ['pipe',
                       'pipe', '&1']
21:50 <@isaacs> piscisaureus_: YEEESSSSSSSSS
21:51 <@isaacs> piscisaureus_: that's *exactly* the API i had in mind.  have
                you hacked into my computer?  are you reading my IMs with
                other people?
21:51 <@isaacs> piscisaureus_: my stupid little naive hack to make this work
                doesn't actually work
21:51 <@piscisaureus_> haha, no I just thought that up a long time ago, and
                       decided not to bother anyone with it
21:51 <@isaacs> i'm getting stderr but not stdout
21:51 <@piscisaureus_> :-)
21:51 <@isaacs> piscisaureus_: i dont 'think you ever told me about it
21:52 <@piscisaureus_> no i don't either
21:52 <@isaacs> piscisaureus_: so it must simply be the Correct api
21:52 <@piscisaureus_> yes
21:52 <@piscisaureus_> so where's our hack
21:52 <@piscisaureus_> s/our/your/
21:57 <@piscisaureus_> isaacs: ^
21:58 < Guest80235> this is the use_fd for stdio options, right?
21:58 < Guest80235> motherfucking freenode
21:58 -!- mode/#libuv [+o tjfontaine] by ChanServ
21:58 < trevnorris> wtf
21:58 <@piscisaureus_> no it should be use_stream or something
21:59 <@piscisaureus_> and specify those twise
21:59 <@piscisaureus_> ^-- ignore brainfart
21:59 <@piscisaureus_> you'd specify the same stream for both stdout and
                       stderr
22:02 <@tjfontaine> I was just presuming use_fd because of the existing dup2
                    code path
22:03 <@piscisaureus_> use_fd requires you to specify the FD that you want
                       inherited
22:03 <@piscisaureus_> if you use libuv to create the pipes that you don't
                       have access to the fd
22:05 <@tjfontaine> well you at least know the position in the array that you
                    passed :P but fair enough
22:09 <@isaacs> piscisaureus_: test: https://gist.github.com/isaacs/5970659
22:09 <@isaacs> piscisaureus_: i already deleted the hack.  it didn't work
                anyway :)
22:10 <@piscisaureus_> isaacs: test first lol

@isaacs
Copy link

isaacs commented Jul 10, 2013

A test of the proposed API: https://gist.github.com/isaacs/5970659

@trevnorris
Copy link

@isaacs with that API would it be possible to pass, say '&4', so you could do like:

// parent
var options = { stdio: [null, '&4', null, 'pipe'] };
var args = [ /* ... */ ];
var proc = child_process.spawn(cmd, args, options);
var pipe = proc.stdio[3];
pipe.write(Buffer('awesome'));

// child
var pipe = new net.Socket({ fd: 3 });
pipe.on('data', function(buf) {
 // do whatever
});

@isaacs
Copy link

isaacs commented Jul 12, 2013

@trevnorris Well, in that example, that'd be an error, because there is no '&4' item in your list. But '&3', I'd expect, would point it at that last 'pipe' stream.

@trevnorris
Copy link

Erm.. Yeah. '&3' is what I meant. :)

@isaacs
Copy link

isaacs commented Jul 13, 2013

Then yes, that would work. Effectively, child.stdio[3] and child.stdio[1] would refer to the same thing, and in the child process.stdio[3] and process.stdio[1] would refer to the same thing.

EDIT: Correct stdio FDs

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants