Setting up a cron job to invoke a PHP script


8/8/02
 

Setting up a cron job

I searched the web for information about how to set up a cron job, and came across >>this help page, from which came this quote:
Firstly you will need to write the script that you want to be executed by
CRON, this can be either in perl, a shell script or PHP. Then create a text
file with the crontab entries you want to have run by cron in the following
format:
  0 * * * * /home/files1/username/scriptname.php
  10 * * * * /home/files1/username/cgi-bin/script2.cgi
  23 10 10 * * /home/files1/username/script.php
This would for example run scriptname.php every hour on the hour and
script2.cgi at 10 minutes past each hour. The last line would run the file
script.php at 23 minutes past 10 on the 10th day of each month. The first 5
entries on each line represent the values of each of the following fields:

  Field:         Minute  Hour  Day of the month  Month  Day of the week
                 ------  ----  ----------------  -----  -------------------
  Allowed values: 0-59   0-23        1-31        1-12   0-7 (0 or 7 is Sun)

Putting a * in means that it will be run on every hour, every day of month,
every month and so on. Once you have created the file (call it something
like cron.tab for ease of use) you can then run the following command:

  crontab cron.tab ...this will load your file command
  crontab -l       ...will list your crontab entries so you can see what
                      is being run and when.

For more help on creating the file to setup crontab type the command:

  man 5 crontab

Invoking a PHP script from a cron job

Then it occurred to me that although this could trigger an executable (.exe) file, I wasn't sure whether it could really trigger a PHP script on SBU's system, since PHP scripts are normally only invoked via web pages. I searched the web some more and came across this >>Google Groups archive entry, from which comes this quote:
Or if you dont want the hassle???? of installing php as a cgi,
a quick and dirty way is just to stick
lynx -dump http://somesite.com/somepage.php > /dev/null
in your crontab. Obviously you must have lynx installed.
 
So I tried this cron.tab file...
0 4 * * * lynx -dump http://somesite.com/somepage.php > /dev/null 
...but got an error message telling me that I wasn't authorised to use cron. I emailed Sally Justice to query this and am now waiting for her to reply.

[Note added 13/9/02: I've just come across this >>How to use Crontab SBU help page - for future reference!]

 
 

<<contents ^top^ next>>