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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...