The other day I was trying get Cron to work with PHP and after a lot of tinkering it worked. I am jotting the steps I used down hear so I don’t forget how I did it and for anyone else who might be interested. Cron is a Unix command that lets you schedule a process to take place at a specified interval. This is really handy if you have some process that you just want to happen every so often automatically, say like backing up a database.
For starters you’ll need to upload your PHP script to your server. I should mention that your script doesn’t have to necessarily be PHP, it can be Perl or another server side scripting languages. This script will be executed automatically by the Cron process you set up.
The next step is to set up a text file with instructions for Cron. I called mine cron.txt. The basic syntax of the Cron instruction file should look something like this:
0,10,20,30,40,50 * * * * /home/directory/www/example.php
The first 5 fields are where you specify how often the process should run. Here is what each of the 5 fields specify:
First field = minutes (0,10,20,30,40,50 used for this example)
Second field = hours of the day (* used for this example)
Third field = days of the month (* used for this example)
Fourth field = months of the year (* used for this example)
Fifth field = days of the week (* used for this example)
If there is a * that means all, i.e. run all days of the week or all hours of the day. In my example I used multiple values for the minutes. I basically told it to run every 10 minutes. The second part of the instructons is the path to where your php script is. After the instructions are set, upload your cron.txt file. I put it in the same directory as my php script.
The next step is to login to the shell of your server and enter a Cron command at the Unix prompt like this:
That basically saves your Cron instructions file. To verify you did this right just enter the following command:
That will list all current cron jobs.
At this point if you did every thing right you should be done.
Unfortunately, the first time around for me it wouldn’t work. To make sure the problem wasn’t with my PHP script, I loaded it up in a web browser to make sure it performs the way it was supposed too. It did, so I knew it was something else. After trying a lot of different approaches, I started to think that the problem was that PHP is meant to be parsed by a web server and that is usually triggered by a web browser. I was getting a lot of “permission denied” errors and I just couldn’t get the script to run through the Cron process. Finally I modified my cron.txt file as follows and it worked:
0,10,20,30,40,50 * * * * /usr/local/bin/php -q /home/directory/www/example.php >> /dev/null
The server’s PHP executable location had to be specified in the cron.txt instruction file . This is the “/usr/local/bin/php -q” part. The -q part means run in quiet mode (i.e. don’t output any HTML headers). The >>.dev/null part means throwaway any output. PHP used normally will provide output to a web browser, but my script just needed to execute. There would be no browser interaction. One of the things I was stuck on the longest was that I had placed the PHP executable location path in the PHP script, but apparently my server didn’t like it there.