<<

Running a PHP script on a regular schedule using curl and cron.

Under normal circumstances a PHP script is only executed when someone requests it via the web by clicking on a or typing the URL into the location bar. However, on occasion, you may want a script to do something a regular interval instead – perhaps to rebuild a “daily graphic” or send nightly email. In these circumstances, you need to trigger the script using a different mechanism than random web access. That’s what this note is about.

The following discussion assumes you have created a PHP script that performs the necessary actions when accessed via the web. In this case, that script is called “cron-demo.php” and is stored in a folder called “cron” off of the user’s root web folder (aka “public_html” on a UW server). [Note: real uw netids need to replace “uwnetid” in each of the three places it occurs below!]

[public_html/cron/cron-demo.php]

// ---- EMAIL script for PHP ------

$to = [email protected]'; $subject = 'Cron Test'; $message = 'Greetings from planet krypton where it is currently ' .date("l dS \of F Y h:i:s A"); $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

($to, $subject, $message, $headers);

print" Script complete.\n";

?>

The next step is to create an script (called a “”) that uses an operating system utility called “curl” to access your PHP script via the web. This is also stored in a folder called “cron” but located just off the user’s “normal” home or root folder (i.e. in “~/cron” not “public_html/cron”.

[~/cron/cron-demo.sh] curl http://students.washington.edu/uwnetid/cron/cron-demo.php

This script needs to be “executable” by you. You can do this in file transfer programs by finding the “get ” display and setting the “owner execute” privilege.

The final step is to create an entry in your “crontab” file of regularly occurring events. You probably don’t have one of these yet, so here’s how you create one.

1. Log in to your account using an SSH Terminal application. You will be prompted to choose one of several commands. “O” for Other and then type “” for the web-dev shell.

2. You will see an operating system prompt ending in a “$” (and abbreviated as $ below). This indicates the computer is waiting for you to tell it what to do. Enter the “export” command shown. This sets your text editor to “pico” for the current session, most people easier to use than the default “” editor.

$ export EDITOR=/usr/local/bin/pico

3. Now, enter the “crontab” command (“-e” is for “edit”)

$ crontab -e

4. Type the following line (or one like it, see below) into the editor, using spaces between the parts, then save it (^o) and (^x), being careful not to modify the default file name in the process.

[your crontab file] 0 1 * * * ~/cron/cron-demo.sh

What that line means: Each crontab entry is a line in this file. Crontab entries provide timing information and the command to execute at the desired . The timing information is divided into “minutes” “hours” “days” “months” and “weekdays” (left to right). So, the above says “run the script at minute 0 of hour 1 of every day of the week and month”. That’s 1:00 AM. If you wanted it to run every hour, you could use a line like this:

0 * * * * ~/cron/cron-demo.sh

If you only wanted “waking hours” (for most of us), you might use

0 6-20 * * * ~/cron/cron-demo.sh

The syntax for hours and minutes allows for “all” (*), “ranges” (1-6), and “lists” (0,15,30,45).

What happens when the script executes: Whenever CRON executes one of your commands as stated in your crontab file, it sends you email with the results. This can be very helpful when debugging things. In the above test case, the PHP script is also sending mail, so if that’s coming to you, expect to get at least two pieces of mail each time the script executes.

CAUTION: This is a bit like “the sorcerer’s apprentice” – i.e., it would obviously be pretty trivial to implement a mail-bomb with the above information (It would also be easy to trace it back to you, so DON’T). Take care with how frequently you have your script execute and whether mail generated by the script goes to the real person requested it, or someone else.

You can view or remove your crontab file using these commands:

$ crontab –l # lists your crontab (must be in “Web-dev”)

$ crontab -r # removes your crontab (must be in “Web-dev”)