cde*_*ker 12 email bash command-line-interface cron
我想编写一个简单的脚本,在日志更改时提醒我。为此,我使用 grep 来查找我感兴趣的行。现在它是这样工作的:
grep line /var/log/file | mail -s Log email@domain.tld
Run Code Online (Sandbox Code Playgroud)
问题是即使没有找到匹配的行,这也会发送邮件。mailutils 的邮件实用程序似乎没有开关告诉它丢弃正文为空的邮件。
有没有一种快速简便的方法呢?
小智 15
“man mail”告诉我,如果正文为空,参数 -E 将停止发送邮件。对我来说很好用。
-E
如果传出消息的第一个或唯一消息部分不包含任何文本,则不要发送它,而是静默丢弃它,在程序启动时有效地设置 skipemptybody 变量。这对于从 cron(8) 启动的脚本发送消息很有用。
ckl*_*orn 12
output=$(grep line /var/log/file); [[ -n "$output" ]] && mail -s Log email@domain.tld
Run Code Online (Sandbox Code Playgroud)
或者你可以把它变成一个 cron 作业,然后如果它产生任何输出,它就会通过电子邮件发送给用户。您可以编辑 /etc/aliases 文件(然后运行 newaliases 命令)以将邮件发送到不在盒子上的地址。
排除 cron 条目(您将无法设置主题行 thogh
1 0 * * * grep line /var/log/file
Run Code Online (Sandbox Code Playgroud)
或者您可以获得 ifne 实用程序 - 这可能是您想要的
grep 行 /var/log/file | ifne mail -s 记录 email@domain.tld
可以从 epel 存储库中获得用于 centos 和 RHEL 的 ifne 命令。我在网上找不到手册页的链接,但它在那里
ifne(1)
ifne(1)NAME ifne - 如果标准输入不为空则运行命令
概要 ifne [-n] 命令
描述 ifne 当且仅当标准输入不为空时才运行以下命令。
选项 -n 反向操作。如果标准输入为空,则运行该命令。
Run Code Online (Sandbox Code Playgroud)Note that if the standard input is not empty, it is passed through ifne in this case.
示例查找。-名称核心| ifne mail -s "Core files found" root
作者 版权所有 2008 Javier Merino
Run Code Online (Sandbox Code Playgroud)Licensed under the GNU GPL 2008-05-01 ifne(1)
归档时间: |
|
查看次数: |
13526 次 |
最近记录: |