2

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

8
  • 1
    This line seems ok * * * * * /usr/bin/php /var/www/mydev/dev/mailServiceTest.php >> /var/www/mydev/CRON_LOG.txt 2> /dev/null. Do you have absolute paths in your script? Is it working properly if executed manually? Commented Apr 15, 2013 at 12:22
  • No my script uses the DOCUMENT_ROOT SERVER super global to include any files that it uses. And yes, when accessed manually through the browser everything seems to work just fine. Commented Apr 15, 2013 at 12:25
  • 1
    You have to use absolute paths in your script as the current working directory may not be the same depending if you are loading the script from your browser or the command line/cron Commented Apr 15, 2013 at 12:27
  • 1
    @OlegTikhonov $_SERVER['DOCUMENT_ROOT'] quite probably doesn't contain what you think it does in the CLI environment. You should construct absolute paths based on __DIR__ (the location of the invoked script) instead. Commented Apr 15, 2013 at 12:33
  • 1
    $_SERVER['DOCUMENT_ROOT'] is not populated when using the cli (but it is when using the browser) :) Commented Apr 15, 2013 at 12:33

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

1
          • 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).

3 Comments

Is it preferable to use namespaces in this kind of scenario. Will it work?
Namespaces have nothing to do with the problem described
Lot's of questions in this, it would be nice if there were answers. Such as "did you verify this was the correct path for php?". How do I verify this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.