grep line /var/log/file | mail -s Log email@domain.tld
问题是,即使没有找到匹配的行,这也会发送邮件.来自mailutils的mail实用程序似乎没有开关告诉它丢弃有空体的邮件.
output=$(grep line /var/log/file); [[ -n "$output" ]] && mail -s Log email@domain.tld
或者您可以将其转换为cron作业,然后如果它产生任何输出,它将通过电子邮件发送给用户.您可以编辑/ etc / aliases文件(然后运行newaliases命令)将邮件发送到不在包装盒上的地址.
cron条目的ex(您将无法设置主题行thogh
1 0 * * * grep line /var/log/file
或者你可以得到ifne实用程序 – 这可能是你想要的
grep line / var / log / file | ifne mail -s Log email@domain.tld
它可以从epel repo for centos和RHEL获得ifne命令.我无法在线找到手册页的链接,但确实如此
ifne(1)
ifne(1)NAME
ifne – Run command if the standard input is not emptySYnopSIS
ifne [-n] commandDESCRIPTION
ifne runs the following command if and only if the standard input is
not empty.OPTIONS
-n Reverse operation. Run the command if the standard input is emp-
ty.06002
EXAMPLE
find . -name core | ifne mail -s “Core files found” rootAUTHOR
copyright 2008 by Javier Merino06003
原文地址:https://www.jb51.cc/bash/386015.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。