I'd like to write a simple script that alerts me if a log changes. For this I'm using grep to find the lines I'm interested in. Right now it works like this:
grep line /var/log/file | mail -s Log [email protected]
Problem is that this sends a mail even if no matching lines are found. The mail utility from GNU Mailutils seems to have no switch telling it to drop mails that have an empty body.
Is there a quick and easy way to do so?
Or you can make this into a cron job and then if it produces any output it will email the users. You can edit the /etc/aliases file (and then run newaliases command) to send mail to address not on the box.
Ex of cron entry (You won't be able to set the subject line thogh
Or you can get the ifne utility - This is probably what you want
grep line /var/log/file | ifne mail -s Log [email protected]
The ifne command it availabe from the epel repo for centos and RHEL. I can't find a link to the man page online but there it is
"man mail" tells me that the argument -E stopps sending mails if body is empty. works fine for me.
See https://unix.stackexchange.com/a/100720/27458
Just use ifne: