1

I have a problem getting a cron job to run (properly).

I created a (very large, but whittled down to nothing) php page that calls mail()

<?
    mail('my@email.com', 'subject', 'test');
    mail('other@address.com', 'subject', 'test');
?>

Then I created my cron job, which runs the file. The line of code in crontab -e is as follows:

12 0 * * * /opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/cron.php

If I run the command /opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/cron.php from terminal, I get an email sent to myself. However, if I run the same line from a cron job, it does not work.

My next stop was to check the logs. I'm running Ubuntu with sSMTP.

Apr 16 11:49:17 drew-Virtual crontab[4722]: (drew) END EDIT (drew) //EDITED CRON

//Calling cron.php file from terminal
Apr 16 11:49:31 drew-Virtual sSMTP[4791]: Creating SSL connection to host
Apr 16 11:49:32 drew-Virtual sSMTP[4791]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:49:34 drew-Virtual sSMTP[4791]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=444
Apr 16 11:49:34 drew-Virtual sSMTP[4794]: Creating SSL connection to host
Apr 16 11:49:35 drew-Virtual sSMTP[4794]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:49:37 drew-Virtual sSMTP[4794]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=454
//I successfully received 2 emails, one to my work account, one to my personal account

//Calling cron.php from cron
Apr 16 11:50:01 drew-Virtual cron[857]: (drew) RELOAD (crontabs/drew)
Apr 16 11:51:01 drew-Virtual CRON[4808]: (drew) CMD (/opt/lampp/bin/php /opt/lampp/htdocs/atlantis/application/controllers/chron.php)
Apr 16 11:51:01 drew-Virtual sSMTP[4810]: Creating SSL connection to host
Apr 16 11:51:02 drew-Virtual sSMTP[4810]: SSL connection using RSA_AES_128_CBC_SHA1
Apr 16 11:51:04 drew-Virtual sSMTP[4810]: Sent mail for drew@drew-Virtual (221 ip-173-201-180-143.ip.secureserver.net closing connection) uid=1000 username=drew outbytes=698
//I did not receive any emails

You can see it only tries to send 1 email, and I assume fails as I never receive it. However, it doesn't tell me why it failed, nor do I have any other clues. I also noticed that the outbytes is larger in the cron job than it is from the command line.

Lastly, the php file has full rwx permissions, for everyone.

Drew
  • 1,014
  • 2
  • 10
  • 21
  • You should ensure `error_reporting` is set to -1, and check the return value of the `mail()` call. – Niels Keurentjes Apr 16 '13 at 16:19
  • Error reporting is on (dev machine and all), and there's no (relevant) errors in the error_log. the mail.log contains the same information as the syslog, mainly that 1 email was sent from cron, or 2 were sent via the CLI. – Drew Apr 16 '13 at 16:30
  • Try dummy debugging - put a dummy echo between all lines to ensure they are all executed. – Niels Keurentjes Apr 16 '13 at 16:32
  • Adding a few lines such as error_log("Running cron.php", 3, "/var/tmp/errors.log"); writes the message to errors.log, but only when run from the command line. Running it from cron doesn't show those lines. – Drew Apr 16 '13 at 16:49
  • Something like `echo '1'; echo mail(...); echo '2'; echo mail(...); echo '3';` will output helpful information such as return values, and which steps were actually executed. – Niels Keurentjes Apr 16 '13 at 16:51
  • I think my ssmtp doesn't like being run as root, and fails silently, in general.I think I also have a problem with my cron script, which causes it to fail when run from cron. When cron fails, it tries to email me (hence the 1 email instead of 2), but that email doesn't send correctly and I never get notified that it fails. I'll update as I figure stuff out – Drew Apr 16 '13 at 17:01

2 Answers2

2

The issue was that PHP was running as an apache module, rather than PHP-CGI. I suppose as a work-around I could have used something like

lynx -dump http://www.somedomain.com/cron.php

For my use, I ended up installing php5-cli and then simply changing the cron job to

php /path/to/file.php

fixed it.

Drew
  • 1,014
  • 2
  • 10
  • 21
0

The best and 100% working solution is to run a corn job with any other page and use the curl function in that page to connect with the page you are using for the email script.