0

I have a simple sh script (file.command) which connects to an Amazon EC2 instance and afterwards performs a command twurl.... (in ruby).

#!/bin/sh

#Connect to Amazon EC2
ssh -i ~/.ssh/research.pem [email protected] twurl -t -d track=keyword  -H stream.twitter.com /1.1/statuses/filter.json

This command sometimes throws a timeout error. How can I rescue this error so that the command doesn't cancel?

Error:

/usr/lib/ruby/1.8/timeout.rb:64:in `rbuf_fill': execution expired (Timeout::Error)
from /usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /usr/lib/ruby/1.8/net/protocol.rb:104:in `read_all'
from /usr/lib/ruby/1.8/net/http.rb:2228:in `read_body_0'
from /usr/lib/ruby/1.8/net/http.rb:2181:in `read_body'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/request_controller.rb:14:in `perform_request'
from /usr/lib/ruby/1.8/net/http.rb:1054:in `request'
from /usr/lib/ruby/1.8/net/http.rb:2144:in `reading_body'
from /usr/lib/ruby/1.8/net/http.rb:1053:in `request'
from /usr/lib/ruby/1.8/net/http.rb:1037:in `request'
from /usr/lib/ruby/1.8/net/http.rb:543:in `start'
from /usr/lib/ruby/1.8/net/http.rb:1035:in `request'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/oauth_client.rb:80:in `perform_request_from_options'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/request_controller.rb:13:in `perform_request'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/request_controller.rb:9:in `dispatch'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/abstract_command_controller.rb:7:in `dispatch'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/cli.rb:38:in `dispatch'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/../lib/twurl/cli.rb:21:in `run'
from /usr/lib/ruby/gems/1.8/gems/twurl-0.8.3/bin/twurl:4
from /usr/bin/twurl:19:in `load'
from /usr/bin/twurl:19
    logout 

Thanks a lot for your help!

08/26/2013 EDIT:

I have written a ruby script which should catch the error, but it seems the method "twurl" isn't recognized in the script, although when running "twurl... " directly on the instance (and not via a script) everything works..

execute_twurl.rb

begin
   twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json)
rescue Exception=>e
    e.inspect
end

called with filename.sh

   #!/bin/sh

    #Connect to Amazon EC2
    ssh -i ~/.ssh/research.pem [email protected] ruby execute_twurl.rb

gives Error:

execute_twurl.rb:2: syntax error, unexpected tUMINUS, expecting kDO or '{' or '('
   twurl -t -d track=keyword -H stre...
             ^
execute_twurl.rb:2: syntax error, unexpected tIDENTIFIER, expecting kDO or '{' or '('
...track=keyword -H stream.twitter.com /1.1/statuses/fil...
                              ^
execute_twurl.rb:2: unknown regexp options - tat
execute_twurl.rb:2: syntax error, unexpected ')', expecting kEND
3
  • Write a Ruby script that will make use of twurl. There you can write your custom rescue handling. Commented Aug 25, 2013 at 19:08
  • Is the ruby script yours? Because to catch a ruby exception you either have to rewrite the twurl code, or you have to write a ruby program that executes the twurl program and then catches the exception thrown by twurl. You can't catch a ruby exception in a shell script. You can certainly check the exit status of the ssh command in your shell script and re-execute your shell command if the remote command failed. Commented Aug 26, 2013 at 1:43
  • The twurl code is not from me - that's the point. Thanks for the hint! Commented Aug 26, 2013 at 12:37

1 Answer 1

3

to run system commands within ruby, you can do the following:

system 'twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json'

this gives you the global variable $? for the exit status of the process which should be 0 if the process didn't succeed. so your ruby script would be something like:

system 'twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json'
if $? == 0
   #retry, throw exeption, etc. 
end

you can also use backticks to run the command and get output status. the following may work for the timeout error.

output=`twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json` ;
result=$?.success?
Sign up to request clarification or add additional context in comments.

2 Comments

Your idea would be perfect but it doesn't work.. seems like $? isn't 0 when the execution expires and the timeout::error is thrown..
I added an option to use backticks that may solve your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.