12

I am trying to run some commands remotely and SSH'ing in to the machine is not an option. What I am trying to do is setup a Sinatra app that runs some specific commands and streams the output through HTTP.

The sample action looks like this:

get "/log" do
  `tail -f some.log`
end

1 As far as I've read, I need to use Unicorn (or Mongrel) because Thin does not support streaming data 2 I think I need to pipe the commands output through some kind of IO ruby object

I almost know how to do (1) but have no idea how to achieve (2).

Kostas
  • 8,356
  • 11
  • 47
  • 63
  • possible duplicate of [Streaming data from Sinatra/Rack application](http://stackoverflow.com/questions/3669674/streaming-data-from-sinatra-rack-application) – Benoit Garret Oct 07 '11 at 10:51
  • The second answer might be worth a shot if you use a recent version of sinatra. – Benoit Garret Oct 07 '11 at 10:52
  • Sinatra 1.3 just released with streaming support see if you can use that http://www.sinatrarb.com/2011/09/30/sinatra-1.3.0?utm_source=rubyweekly&utm_medium=email – Moiz Raja Oct 07 '11 at 10:55
  • @user420504 streaming seems to be working but times-out at 30 seconds – Kostas Oct 07 '11 at 13:09

1 Answers1

12

If you're on a synchronous server (i.e. Mongrel, Unicorn, not Thin), you can just return an IO object:

require 'sinatra'

get '/log' do
  content_type :txt
  IO.popen('tail -f some.log')
end

If that doesn't work (if you're on Thin, for instance), you can use the new streaming API:

require 'sinatra'

get '/log' do
  content_type :txt
  IO.popen('tail -f some.log') do |io|
    stream do |out|
      io.each { |s| out << s }
    end
  end
end

You can also use the bcat gem, which will colorize your output, if it contains ANSI color codes:

require 'sinatra'
require 'bcat'

get '/log' do
  command = %[tail -f some.log]
  bcat = Bcat.new(command, :command => true)
  bcat.to_app.call(env)
end

Note: For infinitely running process you'll have to take care of killing the process yourself if someone closes the connection. With the first solution some servers might take care of that for you.

Konstantin Haase
  • 25,687
  • 2
  • 57
  • 59