英文:
Linux Bash Alert Script /var/log/message
问题
我正在尝试创建一个每隔5分钟在我的Linux机器上运行的警报脚本,我只想要最新的数据有输出,已经通过电子邮件发送的数据不再显示。
tail -n1 /var/log/message | grep suppress | mail-s 'suppress' test@hotmail.com
在上面的命令中,它会一遍又一遍地运行相同的数据,直到我收到一个新的抑制警报。
英文:
I am trying to make an alert script that will run every 5min on my linux machine I only want the latest data to have an output and the data that already emailed will not be show anymore
tail -n1 /var/log/message | grep suppress | mail-s 'suppress' test@hotmail.com
On my command above it will run the same data over and over again until I received a new suppress alert
答案1
得分: 0
以下是翻译好的内容:
当你的脚本每隔5分钟运行一次(可能使用crontab),你可以使用以下命令:
find /var/log/message -mmin -5 -exec grep suppress {} \; |
tail -1 |
mail-s 'suppress' test@hotmail.com
编辑:如何在管道为空时跳过处理?不要使用管道:
msg=$(find /var/log/message -mmin -5 -exec grep suppress {} \; | tail -1)
test -n "$msg" &&
echo "$msg" |
mail-s 'suppress' test@hotmail.com
编辑2:请注意,当日志中出现不带"suppress"的新行时,你将会得到一个旧的"suppress"行。你需要更多的脚本来解决这个问题(记住匹配位置的行号或匹配数并进行比较),还需要找到/var/log/message文件轮转时的解决方案。也许你可以这样做:
msg=$(diff /var/log/message /tmp/oldmessages | grep suppress | tail -1)
test -n "$msg" &&
cp /var/log/message /tmp/oldmessages &&
echo "$msg" |
mail-s 'suppress' test@hotmail.com
英文:
When your scrip runs exactly every 5 minutes (perhaps using crontab), you can use
find /var/log/message -mmin -5 -exec grep suppress {} \; |
tail -1 |
mail-s 'suppress' test@hotmail.com
EDIT: How to skip processing when the pipe is empty? Don't use the pipeline:
msg=$(find /var/log/message -mmin -5 -exec grep suppress {} \; | tail -1)
test -n "$msg" &&
echo "$msg" |
mail-s 'suppress' test@hotmail.com
EDIT 2:
Please note, that you will get an old suppress
line, when the log gets new lines without suppress
in it. You will need more scripting to solve this (remember linenumber where the match was found or nr of matches ad compare) and also find a solution when the /var/log/message file is rotated.
Perhaps you should do something like
msg=$(diff /var/log/message /tmp/oldmessages | grep suppress | tail -1)
test -n "$msg" &&
cp /var/log/message /tmp/oldmessages &&
echo "$msg" |
mail-s 'suppress' test@hotmail.com
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论