php

How to execute a PHP script every 24 hours using a Cron Job

First you'll need to open up the crontab. If you're opening the crontab for the root user you'll use the following command:

$ sudo -e crontab

To execute a PHP script every 24 hours, you can use @daily or 0 0 * * * followed by the location of the PHP binary executable, the -q flag, and the path of the .php file:

0 0 * * * /usr/bin/php -q script_directory/cron.php

If the PHP file is using relative paths, you'll want to make sure you change directory to the folder containing the PHP file:

0 0 * * * cd /script_directory; /usr/bin/php -q script_directory/cron.php

If you need to pass an argument to the PHP file, you can follow it with a key and value:

0 0 * * * cd /script_directory; /usr/bin/php -q script_directory/cron.php someArgument 5

Afterwards, save the crontab and it should start executing right away.

more PHP posts