7

I have the following line in my /etc/cron.d/apache2watch file

* * * * * root /bin/apache2-restart.sh &> /dev/null

I am currently receiving hundreds of mails to root because of this. I don't know why root is still receiving mail every second or so telling me that the cron job was run - even though I have clearly redirected outputs to /dev/null

Can anyone explain why root continues to receive mail, and how I can change this behaviour?

I am running on Ubuntu 10.04 LTS

4 Answers 4

9

You are still receiving emails probably because you have only redirected standard output to /dev/null. Your shell script probably writes to stderr too, so correct redirection would be like:

* * * * * root /bin/apache2-restart.sh > /dev/null 2>&1
2
8

Insert MAILTO="" before you crontab line.

1
  • 1
    it might be worth mentioning that even with this, the output will be retained in a temporary file until the process exits without sending a mail; this may even fill up the filesystem where /tmp is. Feb 6, 2019 at 13:06
3

why don't you set MAILTO="" in particular user's here 'root' crontab. This will disable logging of mail messages in /var/spool/mail/

1
  • Some cronjobs were saved in /var/spool/mail/, I would never found this without you, thank you!
    – Luca Steeb
    Jul 14, 2015 at 12:44
2

You have redirected outputs to /dev/null, but only if you're using bash as your crontab shell. I believe that by default you'd be using /bin/sh.

To fix it, I'd recommend thor's solution:

* * * * * root /bin/apache2-restart.sh > /dev/null 2>&1

The other solution is to change CRON to use bash by putting this at the beginning of your crontab entry:

SHELL=/bin/bash
1
  • this answer is correct – but you might have simply edited thor’s incorrect answer instead Feb 6, 2019 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .