gpt4 book ai didi

ruby - 带有 grep 远程日志文件的 tail

转载 作者:数据小太阳 更新时间:2023-10-29 07:08:32 25 4
gpt4 key购买 nike

我有这段代码来跟踪远程日志文件:

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

我只想在 file.log 中检索带有 ERROR 字符串的行,我正在尝试调用 tail -f#{file} | grep ERROR 但没有成功。

最佳答案

我不确定你想运行 tail -f#{file} | .loop 中的 grep ERROR-f 标志告诉 tail 保持流数据,并且您永远不会使用 Ctrl+C 退出该流。

您可能只想为此使用一个简单的 Bash 命令,为此您甚至不需要 .exec 方法。尝试如下操作:

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

关于ruby - 带有 grep 远程日志文件的 tail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com