Question

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: Why is this running like it isn't 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.

Was it helpful?

Solution 2

Solution: I just remembered I asked last year about Ruby thread behaviour here: Why is this running like it isn't threaded? . I tried running Sinatra using a 1.9.1 interpreter and it worked. Under 1.8.6 it doesn't however.

OTHER TIPS

Try piping /dev/null into your child process:

IO.popen("#{params[:command]} </dev/null") do ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top