1

So, I have a simple app (I have written) which writes to stdout.

When I run it by itself, the output is being printed fine.

Now, when I spawn the process with node... nothing happens (apart from a message saying the process has finished)

Here's my code:

var spawn = require('child_process').spawn;
helper = spawn("myApp", []);
helper.stdout.on('data', function(data) { 
    console.log("GOT: " + data);
});
helper.stdout.on('end', function(data) {
    console.log("FINISHED: " + data);
});
helper.on('exit', function(code) {
    console.log("CLOSED!");
});

And here's the output:

FINISHED: undefined
CLOSED!

Yep, just that.

What's going on? Am I missing something?

6
  • The "end" event won't give you any content. Commented Sep 13, 2015 at 0:06
  • When I change the spawn() to spawn("sh", ["-c", "echo hello world"]) it works fine for me, though I get the "close" event before the data. Commented Sep 13, 2015 at 0:07
  • Well, the end even does fire. The stdout.on('data',...) part isn't... Commented Sep 13, 2015 at 0:09
  • Are you sure that your application code properly flushes its output buffers? (Not super-likely to be the case, but the child process stuff is pretty darned close to a raw wrapper around the underlying system calls.) Commented Sep 13, 2015 at 0:10
  • Hmm... Am I? That's a good question. It's an Objective-C terminal app, fprintfing to stdout... Let me double-check. Commented Sep 13, 2015 at 0:12

1 Answer 1

2

Make sure your stdio output buffers are being flushed in your application:

  fflush(stdout); // or something like that

Output straight to the tty has less buffering than output to a pipe.

Sign up to request clarification or add additional context in comments.

2 Comments

@Dr.Kameleon no problem - fun opportunity to use some stuff I had forgotten I even knew :)
The trouble is that I had forgotten too. I guess there are times like this when it all comes back up... haha.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.