加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 18:41:16 所属栏目:安全 来源:网络整理
导读:我想写一个简单的脚本,如果日志发生变化就会提醒我.为此,我使用grep找到我感兴趣的行.现在它的工作原理如下: grep line /var/log/file | mail -s Log email@domain.tld 问题是,即使没有找到匹配的行,这也会发送邮件.来自mailutils的mail实用程序似乎没有开
我想写一个简单的脚本,如果日志发生变化就会提醒我.为此,我使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读