【问题标题】:tail a big file which is written nonstop尾一个不间断写入的大文件
【发布时间】:2015-05-20 06:33:08
【问题描述】:

我想拖尾一个将不间断写入的日志文件,问题是在我的脚本中我不想指定一些行或字节等。拖尾,我想在我的脚本中指定每次尾随之前没有尾随的最后几行。我怎么能在我的脚本中做到这一点? 谢谢

【问题讨论】:

    标签: linux tail


    【解决方案1】:

    我认为 2010 年的这个答案基本上是要走的路,虽然你没有指定你正在使用的外壳:How to tail -f the latest log file with a given pattern

    本质上,这个想法是保存最后一次拖尾文件的 sn-p,并使用该 sn-p 在文件的下一个尾部输出该 sn-p 之后的所有内容。

    【讨论】:

    • 感谢您的回答。我浏览了您的链接,但我认为我的问题有点不同。我没有多个日志文件,我只有一个,它以每秒 1000 行的速度变大(当然速度变化很大),我希望我的脚本每次都检查最后一行,并且不要t 检查相同的行两次。顺便说一句,我正在使用 bash。
    【解决方案2】:

    我终于这样处理了:

    #!/bin/bash
    
        ###Getting the file size (bytes)
    
        end_of_file=`ls -la /root/mbox | cut -d ' ' -f5`
    
    while sleep 1
    
    do
    
            ### tail from previous end_of_file to the end
    
            tout_finder=`tail -c+$end_of_file /root/mbox | grep -q "desired words" ; echo $?`
    
            if [ $tout_finder == "0" ]; then
    
                    //mycode
    
                     ###getting new size of file
    
                    end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
    
                    ###replacing it with old one
    
                    end_of_file=$end_of_file_new
    
            else
    
                    //mycode
    
                     ###getting new size of file
    
                    end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
    
                    ###replacing it with old one
    
                    end_of_file=$end_of_file_new
    
            fi
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2019-02-03
      • 1970-01-01
      • 2014-10-26
      相关资源
      最近更新 更多