Saturday 3 June 2017

Cannot capture stdout from spawn (nested exec)

I have two scripts A.js and B.js, and a 3rd party application (which is open source). Script A.js launches the 3rd party application using execSync. Script B.js launches script A.js using spawn.

If I run script A.js by itself, I can see the entire output of the 3rd party app. When I run it indirectly by running script B.js, I can only see some of the output.

A.js

const { execSync } = require('child_process');
console.log("Hello");
execSync('mybinary.exe -arg1 -arg2', { stdio: 'inherit' });

B.js

const { spawn } = require('child_process');
const app = spawn(process.execPath, ['A.js'], {
  stdio: ['inherit', null, 'inherit']
});

When I launch B, I only see "Hello", however the binary is running (it's a server and I can open a TCP connection to it). I'm just not seeing the output. When I launch A by itself, I see both "Hello" and the regular binary's output.

If I change B's stdout to 'inherit', it works.

Why is this happening, and how do I fix this?


Environment

  • OS: Windows 10
  • Console: Cygwin (and also tried regular Command Prompt).
  • Node v7.10.0

Note that this is the same issue as here: node.js child_process.spawn no stdout unless 'inherit'



via noahnu

No comments:

Post a Comment