unix - PHP CronJob doesn't work

776

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);
?>
45

Answer

Solution:

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.

891

Answer

Solution:

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.

762

Answer

Solution:

          • [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

People are also looking for solutions to the problem: composer php - Laravel 5.3: PHP Version issue on remote host

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.