Why is crontab not executing my PHP script?

Given

*/30 * * * * php /var/www/html/result.php

There are multiple possibilities why it is not working:

  1. First of all it is important to check if the simple execution of php /var/www/html/result.php. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.

  2. The path of the php binary has to be added.

    */30 * * * * php /var/www/html/result.php
    

    to be changed to

    */30 * * * * /usr/bin/php /var/www/html/result.php
    

    or whatever coming from which php.

  3. Check the permission of the script to the user running the crontab.

    Give execution permission to the file: chmod +x file. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.

  4. To be safer, you can also add the php path in the top of the script, such as:

    #!/usr/bin/php -q
    <?php
    
     ...
    
    ?>
    
  5. Make sure the user has rights to use crontab. Check if he is in the /etc/cron.d/deny file. Also, make a basic test to see if it is a crontanb or php problem.

    * * * * * touch /tmp/hello
    
  6. Output the result of the script to a log file, as William Niu suggested.

    */30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
    
  7. Use the -f option to execute the script:

    */30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
    
  8. Make sure the format in crontab is correct. You can do so for example using the site Crontab.guru.


To sum up, there are many possible reasons. One of them should solve the problem.

Leave a Comment