0

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 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?

3
  • Have you run the ruby code by itself? Do you see the same behavior, or does it run as expected? Commented Aug 1, 2011 at 18:29
  • @pat If I run the ruby code from the command line, type some random text and press enter and it echoes the text and creates a log file in the same directory. Commented Aug 1, 2011 at 18:33
  • Correction: it does create an empty process log file, just not where I expected. I'll update my question. I also find that the ruby file works fine if I leave out the STDIN.each_line part. Commented Aug 1, 2011 at 19:02

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.