Search code examples
phpubuntucroncron-task

Cronjob not executing php?


I want to run a .php every 10 min with a cron job on Ubuntu. This is my crontab:

*/10 * * * * php -f  /var/www/html/gapi/src/test2.php >/dev/null 2>&1

And this is in the log file:

CRON[9994]: (root) CMD (php -f  /var/www/html/gapi/src/test2.php >/dev/null 2>&1)

In this php is an api call, and I can see the api calls live at the dashboard of the api provider, so I know the php is not running every 10 mins.

I set the file permission to 755, what else can I do to make it work?

Updated Crontab:

*/10 * * * * php -f  /var/www/html/gapi/src/test2.php

Solution

  • Try requesting the file through your web server rather than calling the script via the command line PHP interpreter.

    */10 * * * * wget -q -O /dev/null http://localhost/gapi/src/test2.php
    

    (-q to suppress output, -O /dev/null to redirect file output so it doesn't save it)

    or using curl instead:

    */10 * * * * curl --silent http://localhost/gapi/src/test2.php
    

    The URL will depend on how your server is set up - you say it works through your browser at the moment so just use the same URL in the cron file.