在shell脚本中救援ruby命令

时间:2013-08-25 17:03:03

标签: ruby shell amazon-ec2

我有一个简单的sh脚本(file.command),它连接到Amazon EC2实例,然后执行命令twurl....(在ruby中)。

#!/bin/sh

#Connect to Amazon EC2
ssh -i ~/.ssh/research.pem ec2-user@ec2-xx-xx-xx-xxx.compute-1.amazonaws.com twurl -t -d track=keyword  -H stream.twitter.com /1.1/statuses/filter.json

此命令有时会引发超时错误。如何解除此错误以便命令不会取消?

错误:

/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 

非常感谢你的帮助!

08/26/2013编辑:

我编写了一个应该捕获错误的ruby脚本,但似乎脚本中无法识别方法“twurl”,尽管直接在实例上运行“twurl ...”(而不是通过脚本)一切正常..

execute_twurl.rb

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

使用filename.sh调用

   #!/bin/sh

    #Connect to Amazon EC2
    ssh -i ~/.ssh/research.pem ec2-user@ec2-xx-xx-xx-xxx.compute-1.amazonaws.com ruby execute_twurl.rb

给出错误:

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

1 个答案:

答案 0 :(得分:2)

在ruby中运行系统命令,您可以执行以下操作:

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

这给你全局变量$?对于进程的退出状态,如果进程未成功,则应为0。所以你的ruby脚本会是这样的:

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

您还可以使用反引号来运行命令并获取输出状态。以下内容可能适用于超时错误。

output=`twurl -t -d track=keyword -H stream.twitter.com /1.1/statuses/filter.json` ;
result=$?.success?