2010-11-12 23 views
1

我使用system()調用在我的Rails(-v 2.3.5)應用程序中調用C可執行文件。如何在生產模式下在Rails中記錄系統命令輸出?

makefile_path = File.expand_path('./c_executalbe', Rails.root) 
system(makefile_path) 

一切正常,我的本地計算機上,但由於某種原因,system()調用不會在服務器(Dreamhost的)上運行。並且log/production.log中沒有錯誤消息。我希望在日誌中看到該系統調用的返回輸出。我怎樣才能做到這一點?

提前致謝!

回答

3

請參見:http://ruby-doc.org/core/classes/Kernel.html#M005960

用法:

output = %x(command) 
output = `command` 
output = Kernel.send(:`, "command") 

你會得到的命令輸出。

如果command來自一個變量,在您的情況,您可以插值:

output = %x(#{makefile_path}) 
output = `#{makefile_path}` 
output = Kernel.send(:`, makefile_path) 

要登錄入log/production.log

logger.info output 
# or 
puts output 
+0

謝謝!這正是我所期待的。 – saurb 2010-11-14 06:37:53

+0

我無法使用Logger.info(cmd_output)將其記錄在生產上。我正在使用瘦服務器。 – 2012-10-01 18:56:13