mehulkar
mehulkar

Reputation: 4974

How to live stream output from ruby script using child_process.spawn

From what I understand, this should work, but no luck for me so far. Posting this question hoping I'm doing something really silly. I have:

hello.rb

#!/usr/bin/env ruby

3.times do
  puts 'hello'
  sleep 1
end

tail.js

var cp = require('child_process');
var tail = cp.spawn('./hello.rb');

tail.stdout.pipe(process.stdout);

/* Also tried this: */
tail.stdout.on('data', function(data) {
  console.log(data.toString())
});

hello.rb is executable.

When I run node tail.js, 3 hellos are printed to stdout, but after, the loop completes. You can verify this by changing 3.times to just loop for an infinite loop and seeing nothing in stdout. The infinite loop case is actually what I'm attempting to figure out how to do.

What am I doing wrong?

Upvotes: 0

Views: 684

Answers (1)

mscdex
mscdex

Reputation: 106696

You should try either adding $stdout.flush after your puts or by setting $stdout.sync = true at the beginning of your ruby script.

Upvotes: 1

Related Questions