Node.js spawning a child process interactively with separate stdout and stderr streams

前端 未结 3 1123
攒了一身酷
攒了一身酷 2020-12-09 05:08

Consider the following C program (test.c):

#include 

int main() {
  printf(\"string out 1\\n\");
  fprintf(stderr, \"string err 1\\n\");
  ge         


        
3条回答
  •  天涯浪人
    2020-12-09 05:30

    I tried the answer by user568109 but this does not work, which makes sense since the pipe only copies the data between streams. Hence, it only gets to process.stdout when the buffer is flushed... The following appears to work:

    var TEST_EXEC = './test';
    
    var spawn = require('child_process').spawn;
    var test = spawn(TEST_EXEC, [], { stdio: 'inherit' });
    
    //the following is unfortunately not working 
    //test.stdout.on('data', function (data) {
    //  console.log('stdout: ' + data);
    //});
    

    Note that this effectively shares stdio's with the node process. Not sure if you can live with that.

提交回复
热议问题