繁体   English   中英

如果匹配单词,则在Linux上尾随日志文件并触发脚本

[英]Tail a log file on linux and trigger a script if words are matched

我正在寻找一种方法来在Linux上监视多个日志文件,并在其中查找单词或短语,如果找到了它们,则触发脚本或操作,这将需要保持不变。

我知道这可以通过一些grep,tail hack来完成,但是我想知道是否可以使用config选项对此进行预制,例如,我认为logtail可以监视文件但不能触发动作。

有任何想法吗?

您可以将grep的输出设置为变量,然后评估其是否为空以运行脚本/动作。

例:

使用$(将命令输出转换为字符串)
 line=$( grep -m 1 YourKeyWord <( exec tail -f /directory/of/log.out ); kill $! 2> /dev/null) 
然后,您可以开始评估每个日志,并确定以下操作。
 if [ "$line"!="" ] then echo "Found $line" service something start line="" echo "Now we can look for ABC" fi line=$( grep -m 1 ABC <( exec tail -f /your/otherdir/of/log.out ); kill $! 2> /dev/null) if [ "$linea!="" ] then echo "Found the other $linea" ntpstat (or whatever command you need) line="" echo "And we can keep doing this" fi 

您可以使用两个函数来完成此操作(一个使用$ Dir var来重置$ line,另一个使用gir来执行grep),但是为了得到详细的答案,让我们离开这种方式。

线,

grep -m 1 WhateverWord <(exec tail -f /your/otherdir/of/log.out); 杀死$! 2> / dev / null

摘自答案https://superuser.com/questions/275827/how-to-read-one-line-from-tail-f-through-a-pipeline-and-then-terminate ,并作以下解释,并且它确实避免了服务器中的逻辑问题。

“ kill将杀死剩余的tail -f进程,并且我们隐藏了错误,因为在调用kill时,tail可能会消失。”

答案是SEC(百胜安装秒)。 它的作用是监视所有日志文件,并使用规则使用正则表达式扫描文件,然后您可以运行shell脚本,插入日志和其他一些东西。

它作为服务运行,因此计算机重新启动,克隆等都没有问题。

希望这对尝试做我想做的任何人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM