2014-07-21 65 views
1

我需要調用的命令(在西納特拉或Rails應用程序)是這樣的:當命令被執行我可以在Ruby中顯示系統調用的日誌嗎?

`command sub` 

一些日誌將被outputed。 我想查看過程中持續顯示的日誌。

但我把它與完成後,纔可以獲取該日誌的字符串:

result = `command sub` 

那麼,有沒有實現這個的方法嗎?

回答

1

在Windows上我有IO.popen

最佳體驗這裏有一個例子

require 'logger' 

$log = Logger.new("#{__FILE__}.log", 'monthly') 
#here comes the full command line, here it is a java program 
command = %Q{java -jar getscreen.jar #{$userid} #{$password}} 
$log.debug command 
STDOUT.sync = true 
begin 
    # Note the somewhat strange 2> syntax. This denotes the file descriptor to pipe to a file. By convention, 0 is stdin, 1 is stdout, 2 is stderr. 
    IO.popen(command+" 2>&1") do |pipe| 
    pipe.sync = true 
    while str = pipe.gets #for every line the external program returns 
     #do somerthing with the capturted line 
    end 
    end 
rescue => e 
    $log.error "#{__LINE__}:#{e}" 
    $log.error e.backtrace 
end 
+0

這個工程!非常感謝 –

相關問題