Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of child_process.spawn to pipe data #324

Closed
targos opened this issue Oct 14, 2016 · 2 comments
Closed

Use of child_process.spawn to pipe data #324

targos opened this issue Oct 14, 2016 · 2 comments

Comments

@targos
Copy link
Member

targos commented Oct 14, 2016

Hello. I would like to the the following using child_process.spawn:

string (input of command 1) -> command1 | command2 | command3 -> string (output of command3)

I wrote a script like that (simplified):

var spawn = require('child_process').spawn;

var cp1 = spawn('/bin/command1', ['arg1', 'arg2'], {stdio: 'pipe'});
var cp2 = spawn('/bin/command2', ['arg1', 'arg2'], {stdio: [cp1.stdout, 'pipe', 'pipe']});
var cp3 = spawn('/bin/command3', ['arg1', 'arg2'], {stdio: [cp2.stdout, 'pipe', 'pipe']});

var result = '';
cp3.stdout.on('data', data => result += data);
cp3.on('close', () => console.log(result));

cp1.stdin.end('mydata');

The problem is I sometimes get the correct result and sometimes it finishes with an empty string (given the same input data).
Am I doing something obviously wrong ?

@addaleax
Copy link
Member

Am I doing something obviously wrong ?

If it’s any consolation, no, there’s nothing obviously wrong to me, and the code looks about like what I would have written.

I tried turning your example into a runnable script spawning cat, and it seems to work okay…

@targos
Copy link
Member Author

targos commented Nov 6, 2016

Closing in favor of nodejs/node#9413

@targos targos closed this as completed Nov 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants