HowTo: Mail notification after cron task completed.

Dear all,

I have working postfix with gmail account sending emails from system - this is working fine.
I have configured crontab to run “backup” script perodically - this is working fine.

How to configure email notification about this script - the best way is success/failure notification.
Now, I have some configuration done but it not work. My crontab -e:

MAILTO=my@email.com 20 11 * * * /sap_instalki/backup.sh -q -p --retention=3 > /dev/null 2>&1

Regards
GN

You’re redirecting both the stdout and stderr of the job to /dev/null so there’s no output for cron to email you.

Even when I remove /dev/null redirection, still I don’t get any email notification from cron.

Maybe email is being sent but not to where you expect/want. Have you looked in the local mailbox for the user the cronjob runs as? Have you looked in the outgoing mail queue on the machine the cronjob runs on? Have you checked SPAM folders for the recipient email address?

I have fixed my cron configuration - now it works

30 23 * * * /sap_instalki/backup.sh -q -p --retention=7 | mail -s "backup" my@email.com

My concern is - email is sent when backup script is started, but I have no info if this script was success or not.
Kindly advise how to modify contab email notification smth like that:

if backup scrip = success mail root@host -S"backup success" else mail root@host -S"backup failure"

Regards
GN

[QUOTE=gniecka;19809]I have fixed my cron configuration - now it works

30 23 * * * /sap_instalki/backup.sh -q -p --retention=7 | mail -s "backup" my@email.com

My concern is - email is sent when backup script is started, but I have no info if this script was success or not.
Kindly advise how to modify contab email notification smth like that:

if backup scrip = success mail root@host -S"backup success" else mail root@host -S"backup failure"
[/QUOTE]

Doesn’t the output of the script tell you success of failure? With what you have set up you are only going to get the output of stdout emailed to you, so you may not see errors.

Cron will email you both the stderr and stdout from cronjobs. Instead of piping the output of the stdout in to the mail command, you need to get cron to send it’s emails to the desire email address. Who’s crontab is the connjob set in?

If the cronjob is set in /etc/crontab or root’s crontab the output will be emailed to root. You can have a copy of an email sent to root copied to another email address by adding the address to the relevant line in /etc/aliases. If you have a look at that file it explains what to do.

If you really want to do the set up you describe where you get different subject headed emails sent to you depending on success of failure that could be done, but it’s a clunky way of doing it.