PHP is liked by many developers for the ease with which code can be written and applications developed but a question that many people have is, "How to run scripts with cron?" This article addresses this problem as well as serving as a brief introduction to using cron for those new to this capability.
Occasionally you may find that you want to run a script at a specific time, every minute, every hour or every day for example to achieve any number of objectives. You may want to update a website, send out an email or process some other event. To achieve this you have several options.
Manually run a script
The most basic option is simply to run a script manually by accessing it through your browser. This is fine if you would be at your site anyway, writing and then sending out an email for example, but just isn't feasible if you want to run a script frequently or at odd times during the night.
Include the script in another file
You could include your script in another file on your website that is frequently accessed by your visitors, your index file for example. If you don't have many visitors though your script isn't going to be run at the times you want it run and if you do have lots of visitors you will likely massively increase your server load.
This approach could work if your script doesn't need to be run at defined times and could be ideal if it is updating content for your visitors. In this sense it could work in a manner very similar to a cache, fetching data from a local filesystem ordinarily and only getting data from elsewhere when the local file is out-of-date.
With careful scripting this approach will work for some problems but certainly not all and a different approach that doesn't rely on your visitors is needed.
Crontab
Enter cron! Cron is normally available on all linux and unix distros. It is a daemon that allows you to schedule a script or program to run at a specific time.
If you are running your own server you'll need to add a line similar to the one below to your /etc/crontab file. If you are using shared hosting there should be an option in your control panel to set up cron tasks, if not, then you'll need to speak with your admin. The instruction you'll need to add will look something like this:
lynx -dump http://www.yoursite.com/cron_script.php
This line will run your script every minute as indicated by the five stars at the beginning (I explain what these five stars signify at the end of the article). Every minute a small web browser called lynx will be run which will open the url supplied to it, in this case your script.
If you run your own server to get things going you'll have to enter the following in the shell:
Shell> crontab crontab
That starts the cron daemon which will then read instructions from /etc/crontab and execute them at the appropriate times. If you are using shared hosting the cron daemon should already be running.
Scheduling with Cron
Earlier in the article I skipped over the slightly cryptic first five values (all stars in the example above) in the crontab entry but now I'm going to go into a little bit more detail. Going from left to right the values correspond to minute, hour, day of month, month, and then day of week.
[minute] [hour] [day of month] [month] [day of week] [command]
The values entered are either numerical values, a comma-seperated list of numerical values or a star (*) to signify all values. In the example above all the values were stars and so the command would have run every minute.
As a further example lets say we want to run a script that sends out an email containing an April fools joke to a list of subscribers. We'll send it out on the 1st April at 3am (because we think demand on the server should be low around then) and our crontab entry would look something like this:
00 3 1 4 lynx -dump http://www.yoursite.com/april_fools_script.php