击发送邮件只有当文件包含字符串 [英] Bash send mail only if file contains string

查看:215
本文介绍了击发送邮件只有当文件包含字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个过程创建每次运行时新的日志文件。如果日志文件包含字符串错误,我希望它使用的邮件通过电子邮件发送给我。我不想要的电子邮件,如果有任何错误。

在创建别名.mailrc文件名为管理员的收件人

此命令工作正常总是通过电子邮件发送日志:

 猫/home/user/import.log |邮件-s导入错误日志管理员

现在那么我要的是一个连接的命令检查该文件包含错误然后运行mail命令只有当它。

先谢谢了。


解决方案

 如果比fgrep错误/home/user/import.log;然后
    邮件-s导入错误日志管理员
科幻

A process creates a new log file each time it runs. If the log file contains the string ERROR, I want it to be emailed to me using mail. I do not want an email if there were no errors.

Created alias named admins in .mailrc for recipients

This command works fine to always send the log via email:

cat /home/user/import.log | mail -s 'Import Error Log' admins

Now then what I want is a concatenated command to check if the file contains 'ERROR' then run the mail command only if it does.

Thanks in advance.

解决方案

if fgrep ERROR /home/user/import.log; then
    mail -s 'Import Error Log' admins
fi

这篇关于击发送邮件只有当文件包含字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆