I set up a crontab. I've installed AIDE and I have an AIDE database to check for file integrity.
How do I get cron to email me ONLY when files have been modified?
The script:
#!/bin/bash
if aide -c /etc/aide/aide.conf --check
then echo "AIDE detected no changes"
else
echo "Alert!: AIDE detected changes!"
The crontab:
* */12 * * * /root/script.sh | mail [email protected]
Use the
MAILTO
crontab variable rather then piping tomail
. Then change your script so that it doesn't output anything unless there is a problem:The crontab:
Notes:
root
.MAILTO=""
is to stop following cron rules from sending mails. If you want them to do that, leave it out. It must beMAILTO=""
notMAILTO=
. (Cron is not implementing shell syntax here. Another clue is that the you can put spaces around the=
which you can't do with shell syntax.)crontab
is not part of the POSIX spec. There have been many implementations ofcron
over the years and not all of them support variable setting. Check whatman 5 crontab
says on your system.Alternatively, you could use
... | mailx -E [email protected]
rather than... | mail [email protected]
. This will skip sending the mail if the body (i.e. stdin) is empty.