The tool you use to schedule things in unix-style operating systems is cron
. If your server runs MS Windows, you'll need to use some other scheduler.
There are a couple of ways that you can use cron
to run your script.
First off, if you have a CLI version of PHP available (as opposed to an Apache module or a fcgi version), you can run it directly. On my system, a cron job to run the script once per minute might look like this:
* * * * * /usr/local/bin/php /path/to/your/script.php
Note that on your system, the php
binary might be located in /usr/bin/
or /opt/local/bin/
or somewhere else. You'll need to check with your operating system's package repository to find out where, or type which php
from a terminal.
If you use a "shebang" at the beginning of your script, you can get away with a simpler cron job:
* * * * * /path/to/your/script.php
More information about running PHP shell scripts can be found here. A word of warning though... If you launch scripts directly like this, they will run as the user who owns the cron job, which may have security (or at least permission) implications. If you don't understand what this means, please ask more questions before doing it!
If you don't have a command line PHP available, you can still have the PHP script run via cron, by fetching it from a command-line HTTP client like curl
or wget
or fetch
(depending on your operating system). For example:
* * * * * curl -o /dev/null http://example.com/path/to/script.php
or
* * * * * wget -O /dev/null http://example.com/path/to/script.php
On a unix system you can man crontab
for instructions on how to install cron jobs, and probably man 5 crontab
(depending on your operating system) for instructions on the format for setting specific times. Note that cron uses 1-minute granularity. If you want greater frequency than that, you can fake it. For example, for three times per minute:
* * * * * /path/to/your/script.php
* * * * * sleep 20; /path/to/your/script.php
* * * * * sleep 40; /path/to/your/script.php
And if you want lesser frequency, you use crontab's documented format. For example, every five minutes on weekdays but every 10 minutes on weekends:
*/5 * * * 1-5 /path/to/your/script.php
*/10 * * * 6-7 /path/to/your/script.php