I'm testing the communication between node.js and executables launched as child processes. An executable will be launched from within node.js via child_process.spawn()
and its output will be monitored by node.js. I'm testing this capability both on Linux and Windows OSs.
I've successfully spawned tail -f /var/log/syslog
and listened to its output, but my own executables can't seem to write correctly to stdout (in whatever form it exists when captured by node.js).
Test code:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
int main()
{
using namespace std;
long x = 1;
while (true)
{
fprintf(stdout, "xtime - %ld\n", x++);
usleep(1000000);
}
}
(Note: some include
s may be useless; I've not checked them)
stdout
output is not automatically flushed (at least on *nix) when stdout
is not a tty (even if there is a newline in the output, otherwise a newline generally flushes when stdout
is a tty).
So you can either disable stdout
buffering entirely via setbuf(stdout, NULL);
or you can manually flush output via fflush(stdout);
.