# HowTo: Mail notification after cron task completed.

**URL:** <https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238>\
**Category:** SLES Configure-Administer\
**Created:** [March 10, 2014, 12:39pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238 "2014-03-10T12:39:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gniecka](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gniecka](https://forums.suse.com/u/gniecka)\
**Post date:** [March 10, 2014, 12:39pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/1 "2014-03-10T12:39:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikewillis](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mikewillis](https://forums.suse.com/u/mikewillis)\
**Post date:** [March 10, 2014, 2:22pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/2 "2014-03-10T14:22:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gniecka](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gniecka](https://forums.suse.com/u/gniecka)\
**Post date:** [March 10, 2014, 3:32pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/3 "2014-03-10T15:32:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikewillis](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mikewillis](https://forums.suse.com/u/mikewillis)\
**Post date:** [March 10, 2014, 3:52pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/4 "2014-03-10T15:52:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gniecka](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@gniecka](https://forums.suse.com/u/gniecka)\
**Post date:** [March 11, 2014, 11:30am UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/5 "2014-03-11T11:30:53Z")

</div>

I have fixed my cron configuration - now it works

```auto
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

---

<div class="post-metadata">

**Author:** ![mikewillis](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@mikewillis](https://forums.suse.com/u/mikewillis)\
**Post date:** [March 11, 2014, 3:07pm UTC](https://forums.suse.com/t/howto-mail-notification-after-cron-task-completed/25238/6 "2014-03-11T15:07:03Z")

</div>

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

```auto
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.
