【问题标题】:Run tail -f for a specific time in bash script在 bash 脚本中运行 tail -f 特定时间
【发布时间】:2013-05-03 22:19:29
【问题描述】:

我需要一个脚本来运行一系列tail -f 命令并将它们输出到一个文件中。 我需要的是tail -f 运行一段时间来 grep 特定的单词。之所以需要一定时间,是因为其中一些值不会立即显示,因为这是实时日志。

我怎样才能运行这样的东西,比如说 20 秒,输出 grep 命令,然后继续执行下一个命令?

tail -f /example/logs/auditlog | grep test

谢谢

【问题讨论】:

    标签: linux bash unix tail


    【解决方案1】:
    timeout 20 tail -f /example/logs/auditlog | grep test
    

    【讨论】:

      【解决方案2】:
      tail -f /example/logs/auditlog | grep test &
      pid=$!
      sleep 20
      kill $pid
      

      【讨论】:

        【解决方案3】:

        这个呢:

        for (( N=0; $N < 20 ; N++)) ; do tail -f /example/logs/auditlog | grep test ; sleep 1 ; done
        

        编辑:对不起,我误读了您的问题。你想要这样的东西:

        tail -f /example/logs/auditlog | grep test
        sleep 20
        

        【讨论】:

          猜你喜欢
          • 2022-10-14
          • 1970-01-01
          • 2020-06-07
          • 1970-01-01
          • 2019-04-18
          • 2011-01-03
          • 2014-03-15
          • 2020-10-24
          相关资源
          最近更新 更多