在 bash 脚本中运行 tail -f 特定时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16368382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:21:08  来源:igfitidea点击:

Run tail -f for a specific time in bash script

linuxbashunixtail

提问by user2348517

I need a script that will run a series of tail -fcommands and output them into a file. What I need is for tail -fto run for a certain amount of time to grep specific words. The reason it's a certain amount of time is because some of these values don't show up right away as this is a live log.

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

How can I run something like this for let's say 20 seconds, output the grep command and then continue on to the next command?

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

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

Thanks

谢谢

回答by Karoly Horvath

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

回答by Chris Dodd

tail -f /example/logs/auditlog | grep test &
pid=$!
sleep 20
kill $pid

回答by hannenz

What about this:

那这个呢:

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

EDIT: I misread your question, sorry. You want something like this:

编辑:我误读了你的问题,抱歉。你想要这样的东西:

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