Node.js ruby ​​subprocess freezes

Using the example here, I am trying to create a ruby ​​process (v1.8.7) from Node.js (v0.4.8). A process and an empty log file are created, but nothing happens until I kill it. If I leave a bit STDIN.each_line, the code will work fine.

I suspect stdin does not end, so ruby ​​is still waiting for input. Perhaps ruby.stdin.write("ping\n");not doing what I think he should do?

-1
source share
1 answer

Here is what I have, it seems to do what you want:

var util  = require('util'),
    sys   = require('sys'),
    spawn = require('child_process').spawn,
    ruby  = spawn('ruby', [__dirname + '/process.rb']);

function trim(str) {
  return str.replace(/^\s+|\s+$/, '');
}

ruby.stdout.on('data', function(data) {
  console.log('stdout: ' + trim(data.toString()));
});

ruby.stdout.on('end', function(data) {
  ruby.stdout.flush();
});

ruby.stderr.on('data', function(data) {
  console.log('stderr: ' + data);
});

ruby.on('exit', function(code, signal) {
  if(code != null) console.log('exit: ' + code);
  else if(signal != null) console.log('killed: ' + signal);
});

ruby.stdin.write("ping\n");
ruby.stdin.end();
+1
source

All Articles