Peeyush Kushwaha
Peeyush Kushwaha

Reputation: 3623

PHP CronJob doesn't work

I'm trying to set up a cronjob which sends me an email when it runs.

when I execute the file using a ssh command via PuTTY, it works, it also works when I set it up as an URL, but doesn't work when setting using absolute path.

Here is what all I've tried till now : Does work :

    *   *   *   *   *   http://example.com/cron/cron.php

Doesn't work :

    *   *   *   *   *   php /var/www/clients/client2/web6/web/global/cron/cron.php

    *   *   *   *   *   /var/www/clients/client2/web6/web/global/cron/cron.php

    *   *   *   *   *   /usr/bin/php /var/www/clients/client2/web6/web/global/cron/cron.php

All 3 that I listed in 'doesn't work' works when executed as commands through PuTTY

File permissions are all sorted out and are 777 for the cron file, and the directory it resides in.

I'm using IspConfig3 and Debian and Apache2, if that matters

EDIT : here's the code in cron.php :

#!/usr/bin/php
<?php
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Cronjob <[email protected]>' . "\r\n";
mail("[email protected]", "Cronjob Complete - ". date('d-m-Y'), "At".date("H:i:s"), $headers);
?>

Upvotes: 0

Views: 2138

Answers (3)

Sam Ham
Sam Ham

Reputation: 111

Run this to find the directory:

 <?php print __FILE__; ?>

Try this:

*   *   *   *   * php /var/www/clients/client2/web6/web/global/cron/cron.php

This is how my crons work on my website.

Upvotes: 1

Andrej  Bestuzhev
Andrej Bestuzhev

Reputation: 674

          • [USER] /usr/local/bin/php /var/www/clients/client2/web6/web/global/cron/cron.php

User must have rights to execute your script. Also, if it not helps, check logs: tail -f /var/log/cron

Upvotes: 1

Thomas Huijzer
Thomas Huijzer

Reputation: 364

You probably have some relative includes in your PHP file. So:

  • Change the paths in your PHP file to absolute paths,
  • or first move to the dir in your cronjob and then execute the PHP file. You can do this by executing the cronjob like this: cd /var/www/clients/client2/web6/web/global/cron/; php cron.php

Hope this helps.

Upvotes: 1

Related Questions