0

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.

3
  • I think you meant "The thing is, if I enter a command which expects input..." didn't you? Commented Feb 27, 2010 at 10:04
  • Do you wait for threads to finish with join later on? This piece of code seems to be ok to me. Commented Feb 27, 2010 at 10:38
  • @vava I edited the question with the answer. Commented Feb 27, 2010 at 10:46

2 Answers 2

0

Try piping /dev/null into your child process:

IO.popen("#{params[:command]} </dev/null") do ... 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.