微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

email – 如果正文不为空,则从命令行发送邮件

我想写一个简单的脚本,如果日志发生变化就会提醒我.为此,我使用grep找到我感兴趣的行.现在它的工作原理如下:
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 empty

SYnopSIS
ifne [-n] command

DESCRIPTION
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” root

AUTHOR
copyright 2008 by Javier Merino

06003

原文地址:https://www.jb51.cc/bash/386015.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐