0

我有一个简单的 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 

非常感谢你的帮助!

2013 年 8 月 26 日编辑:

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

执行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
4

1 回答 1

2

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

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

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

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?
于 2013-08-27T06:35:36.977 回答