Oleg Belousov
Oleg Belousov

Reputation: 10111

crontab not running a php script properly

I am trying to run an automated mailing service based on SendGrid and cron. I've tried all of the possible combinations to make the PHP interpreter to run my script, and although the cron log says that the job is executed each minute, the log file which I defined remains empty, and no emails are received, and yet everything works fine when I access the page via the browser.

mydev.com is a virtual host that I am using on my machine, and I haven't made any changes to the php configuration(max execution time etc).

Also, i did my research online, and I have php5-cli installed, and all of the requited dependencies.

below is the lines I added to the cron file after typing sudo crontab -e in the terminal:

* * * * * php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null
* * * * * /usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null
* * * * * /usr/bin/php http://mydev.com/dev/mailServiceTest.php   >> /var/www/mydev/CRON_LOG.txt 2> /dev/null

* * * * * /usr/bin/lynx http://mydev.com/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null

And here is the output on the cron log:

Apr 15 15:13:01 oleg-Lenovo-G580 CRON[8948]: (root) CMD (/usr/bin/php http://mydev.com/dev/mailServiceTest.php   >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:13:01 oleg-Lenovo-G580 CRON[8947]: (root) CMD (php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:13:01 oleg-Lenovo-G580 CRON[8949]: (root) CMD (/usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:13:01 oleg-Lenovo-G580 CRON[8953]: (root) CMD (/usr/bin/lynx http://mydev.com/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:14:01 oleg-Lenovo-G580 CRON[8964]: (root) CMD (/usr/bin/lynx http://mydev.com/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:14:01 oleg-Lenovo-G580 CRON[8966]: (root) CMD (php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:14:01 oleg-Lenovo-G580 CRON[8965]: (root) CMD (/usr/bin/php http://mydev.com/dev/mailServiceTest.php   >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:14:01 oleg-Lenovo-G580 CRON[8970]: (root) CMD (/usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:15:01 oleg-Lenovo-G580 CRON[8992]: (root) CMD (/usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:15:01 oleg-Lenovo-G580 CRON[8994]: (root) CMD (php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:15:01 oleg-Lenovo-G580 CRON[8993]: (root) CMD (/usr/bin/php http://mydev.com/dev/mailServiceTest.php   >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:15:01 oleg-Lenovo-G580 CRON[8998]: (root) CMD (/usr/bin/lynx http://mydev.com/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:16:01 oleg-Lenovo-G580 CRON[9009]: (root) CMD (/usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:16:01 oleg-Lenovo-G580 CRON[9011]: (root) CMD (php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:16:01 oleg-Lenovo-G580 CRON[9012]: (root) CMD (/usr/bin/lynx http://mydev.com/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)
Apr 15 15:16:01 oleg-Lenovo-G580 CRON[9010]: (root) CMD (/usr/bin/php http://mydev.com/dev/mailServiceTest.php   >> /var/www/mydev/CRON_LOG.txt 2> /dev/null)

Any help is much appreciated!.

O.

EDIT:

Below is the code that is supposed to be executed:

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/mailingService.php');

$myMail = new mailingService();
$myMail->execute();
$myMail->outputDuration();

?>

Now, even if I add 'echo 1;' at the beginning of the file, or at the end of it, nothing changes much, and the output file CRON_LOG.txt remains empty

Upvotes: 2

Views: 2719

Answers (2)

symcbean
symcbean

Reputation: 48387

          • php /var/www/mydev/dev/mailServiceTest.php

Is 'php' in the cron daemon's path?

          • /usr/bin/php /var/www/mydev/dev/mailServiceTest.php

Better (did you verify this was the correct path for php?

Mostly this makes sense - but if you need to run any command with a frequency of once / minute then running it via cron is far from an ideal solution.

my script uses the DOCUMENT_ROOT SERVER super global

But that's meaningless / null for the CLI SAPI

This is not going to do what you expect.

Might work, but lynx is intended as a user-driven browser, and possibly won't even start in this context - wget or curl would be better.

From the cron log crond is doing exactly what it should.

That you are throwing away the STDERR means you've got no visibility of a lot of error messages. And you're also not writing anything useful to STDOUT. Is the timestamp on the target log files being updated?

Did you install the crontab file or just edit it?

If you need DOCUMENT_ROOT then you need to access the script via HTTP - check that your system can resolve it's own name and use wget or curl (i.e. try wget http://mydev.com/dev/mailServiceTest.php from an ssh session).

Upvotes: 1

Andrej  Bestuzhev
Andrej Bestuzhev

Reputation: 674

$_SERVER['DOCUMENT_ROOT'] - it's web server's variable. CRON don't uses apache when executing scripts.

Upvotes: 5

Related Questions