1

I'm having a strange problem here with crontab. I have set up the crontab correctly to run the cron job as follows:

[user@user ~]$ crontab -e
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=myemail@mydomain.co.uk
HOME=/

2 * * * * php usr/share/nginx/html/site/script.php

The script runs, I know this because the script is just a basic test that uses the mail() function to let me know it ran.

But I do not get the e-mail from crontab to let me know the script has ran. I should be getting an output e-mail to the MAILTO address above but I am not. I have even tried with quotes around the address.

There's nothing wrong with Sendmail because otherwise the script wouldn't be able to send an e-mail. There's nothing wrong with the e-mail address either because the mail() code in script.php sends to the same e-mail address.

Technically I could just send the e-mail output myself this way rather than rely on crontab but I know that's not the right way to do it.

Any idea where I am going wrong?

PS. This is a Centos 6.3 server.

Xenor
  • 27
  • 1
  • 6

2 Answers2

3

What you can do is to remove that MAILTO and then just use a root forwarder.

/root/.forward <- just place your email in there, nothing else. Assuming you are running the cron job as root. You can also do it via /etc/aliases, add the line: user: my@email.com

Emil
  • 53
  • 4
0

I would check cron and mail log to see whether there is any error reported. Could that be also possible the email is landed in your junk mail folder or recipient mail server rejected the mail for invalid sender name or hostname etc.,?

Jaime Hablutzel
  • 416
  • 4
  • 10
antimatter
  • 209
  • 1
  • 7
  • There's nothing in the cron log, just a line saying the command was ran. It's not a junk folder issue either, been checking that and I've also tried directing the output to an e-mail address at a different provider. I did have to edit the mail config to bypass local e-mail check though as shown here http://serverfault.com/questions/65365/disable-local-delivery-in-sendmail because php mail(); wasn't working at all on my the server's own domain, I wonder if that broke it. – Xenor Apr 21 '13 at 14:50
  • 2
    Please do a live tail of mail log when your cron runs and see what is happening. You should see where is sendmail relaying to. tail -f /var/log/maillog – antimatter Apr 21 '13 at 15:12
  • 1
    I've no idea why but it started working around 5pm yesterday. I think I did reboot my server late afternoon so maybe that changed something. Or the MAILFROM parameter I added. – Xenor Apr 22 '13 at 20:14
  • Checking the mail log (in my case `/var/log/exim4/mainlog`) allowed me to spot the problem which was that I had a stale `root: myuser` entry in `/etc/aliases` and the user `myuser` didn't exist anymore. – Jaime Hablutzel Apr 06 '20 at 02:59