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

前端 未结 3 1125
攒了一身酷
攒了一身酷 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:42

    You can do this :

    var TEST_EXEC = 'test';
    var spawn = require('child_process').spawn;
    var test = spawn(TEST_EXEC);
    
    test.stdin.pipe(process.stdin);
    test.stdout.pipe(process.stdout);
    test.stderr.pipe(process.stderr);
    

    When you use events on stdout and stderr to print the output on console.log, you will get jumbled output because of asynchronous execution of the functions. The output will be ordered for a stream independently, but output can still get interleaved among stdin,stdout and stderr.

提交回复
热议问题