views:

57

answers:

1

I'm playing around with Rails & Sinatra, and I want to execute commands on the server. Those commands are entered from a form. The thing is, if I enter a command which expects input, my whole app hangs. Here's the code I'm using to execute them:

@threads << Thread.new do
    Thread.current["buffer"] = ""
    puts "starting #{params[:command]}"
    IO.popen(params[:command]) do |io|
        io.each_line {|l| Thread.current["buffer"] += l}
    end
end

this works ok for simple commands like ls ... but for example if I enter pause which will expect the user to press a key to continue, everything hangs. How can I get around that?

EDIT: I just remembered I asked last year about Ruby thread behaviour here: http://stackoverflow.com/questions/1383470/why-is-this-running-like-it-isnt-threaded . I tried running Sinatra using a 1.9.1 interpreter and it worked. Under 1.8.6 it doesn't however. A mod can close this question if he wants.

A: 

Try piping /dev/null into your child process:

IO.popen("#{params[:command]} </dev/null") do ...
hurikhan77