2013-08-25 131 views
0

我有一個簡單的sh腳本(file.command),它連接到Amazon EC2實例,然後執行命令twurl....(在ruby中)。shell腳本中的救援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 

此命令有時會引發超時錯誤。我該如何解救這個錯誤,以便命令不會取消?

錯誤:

/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 ......「直接在實例(而不是通過腳本)一切正常..

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 [email protected] 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 
+0

編寫一個將使用twurl的Ruby腳本。在那裏你可以寫你的自定義救援處理。 –

+0

是你的Ruby腳本嗎?因爲要捕獲ruby異常,你必須重寫twurl代碼,或者你必須編寫一個ruby程序來執行twurl程序,然後捕獲twurl拋出的異常。你不能在shell腳本中捕獲ruby異常。您當然可以在shell腳本中檢查ssh命令的退出狀態,並在遠程命令失敗時重新執行shell命令。 – 7stud

+0

twurl代碼不是來自我 - 這就是要點。感謝提示! – SPi

回答

1

運行中的紅寶石系統命令,就可以執行以下操作:

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? 
+0

你的想法是完美的,但它不起作用..看起來像$?當執行過期並且引發timeout :: error時,它不是0 .. – SPi

+0

我添加了一個選項來使用反引號,可以解決你的問題 –