Tail with grep remote log files

I have this code for the tail of deleted log files:

def do_tail( session, file ) session.open_channel do |channel| channel.on_data do |ch, data| puts "[#{file}] -> #{data}" end channel.exec "tail -f #{file}" end Net::SSH.start("host", "user", :password => "passwd") do |session| do_tail session, "/path_to_log/file.log" session.loop 

I want to get only the lines with the string "ERROR" in the .log file, I try to call "tail -f # {file} | grep ERROR", but to no avail.

Thanks in advance.

+6
source share
1 answer

I'm not sure if you want to run tail -f #{file} | grep ERROR tail -f #{file} | grep ERROR inside .loop . The -f flag tells tail to save stream data, and you never exit this stream with Ctrl + C.

You may need to simply use the Bash command for this, for which you do not even need the .exec method. Try the following:

 def do_tail session, file @keyword = 'ERROR' session.open_channel do |channel| channel.on_data {|ch, data| puts "[#{file}] -> #{data}"} `grep #{keyword} #{file}` end end 
0
source

All Articles