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']
});
app.stdout.on('data', (chunk) => {
process.stdout.write(chunk);
});
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?
UPDATE: Doesn't seem to have anything to do with the use of two scripts. Spawning the binary directly without using stdio inherit exhibits the same issue. Seems to be dependant on the third party application. How could a program affect node if it just prints to stdout?
CONTEXT: I want to launch a server and then run a command when the server is ready. When ready, the server prints out a particular message. The idea was to wait for this message to be printed. After printing out this message, it doesn't print out anything else, but it stays running.
The particular executable I'm attempting to pipe is a ChatScript server.
UPDATE: At this point, I assume it's something with the specific binary I'm attempting to pipe. I want to understand how the binary can affect this behaviour, i.e. works with inherit but not pipe. If someone can recreate this behaviour with a node script (or c program), that will answer the question.
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'