开发者

Node.js ruby subprocess hangs

开发者 https://www.devze.com 2023-03-25 16:03 出处:网络
Based on the example here, I\'m trying to spawn 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 out the STDI

Based on the example here, I'm trying to spawn 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 out the STDIN.each_line bit, the code runs fine.

I 开发者_StackOverflow社区suspect something stdin doesn't finish so ruby is still waiting for input. Perhaps ruby.stdin.write("ping\n"); doesn't do what I think it should do?


Here's what I have that 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消