phpbbDoctor Site Admin
Joined: 12 Apr 2005
             Posts: 269

|
Subject: [How to] Use "cron" to perform automatic tasks Sat Aug 27, 2005 8:41 am |
|
|
|
Ever needed to run a particular Unix command, PHP or Perl script regularly? You obviously can't be bothered to run it manually every so often (especially if it has to be run daily!). So what are you going to do? Use cron! Cron is a Unix scheduling utility. The syntax is initially a bit cryptic, but it is fairly straight-forward. Once you have it figured out, it's extremely powerful. You'll need to either have telnet/ssh (shell) access to your server, or some other way to interface with cron. Many control panel applications (like CPanel) offer the ability to manage cron jobs. You'll also have to have a host which allows you to actually run cron jobs.
For example, we have the following entries in our crontab (cron table) for a specific forum that we manage:
Code: | 42 2 * * 0 /usr/bin/zip -q access-log.`/bin/date +\%m\%d`.zip access-log
*/5 * * * * /home/username/cronfiles/stats.sh
57 * * * * /home/username/cronfiles/hourly.sh
59 23 * * * /home/username/cronfiles/daily.sh
52 1 * * 6 /home/username/cronfiles/weekly.sh |
We've changed the path names to something generic; you would need to know the exact path that would be appropriate for your server. Reading from left to right, the first few symbols (numbers or *) refer to when a job will run. For example, the fourth entry in the crontab listed above runs at 23:59 (that's 11:59 pm). Since the next three items are *, it runs on every day on every month, no matter which day of the week it is. The first job zips up the access logs for the web site, at 2:42 AM, but only on Sunday. It doesn't matter which day of the month it occurs on, it just happens every Sunday.
After that cryptic beginning, the rest of the line is just the command to run. So suppose you wanted something to run on the 15th at noon, but only if it was a Saturday. Then your cron entry would look something like:
Code: | 0 12 15 * 6 your_job_here |
When cron looks for jobs to run, this job will only run at noon (12:00) on the 15th (15) of any month (*) when that day is a Saturday (6). In other words, not very often.
The five fields, in order, are minute (valid values: 0 - 59), hour (valid values: 0 - 23), day of month (valid values: 1 - 31), month (valid values: 1 - 12, or use names), and day (valid values: 0-7, Sunday is 0 or 7, or use names). Any of these fields can be a *, which means match all values. You can also use a comma-seperated list of values, like this: 1,2,3. Or what about this entry?
Code: | */5 * * * * /home/username/cronfiles/stats.sh |
The first item in that entry is a shortcut for "every five minutes". The */5 takes the system time and determines if it is an even multiple of 5. If so, the job is run. We use this to run an update to the forum statistics every five minutes throughout the day, rather than having each user run a query for every page that they view. The same thing could have been written this way:
Code: | 0,5,10,15,20,25,30,35,40,50,55 * * * * /home/username/cronfiles/stats.sh |
In our opinion, the shortcut version is easier to read, and is actually more robust. If you look carefully at the example above, one of the five-minute interval numbers is missing. It might be difficult to figure that problem out. By using the shortcut of */5 we are guaranteed not to miss any five-minute interval value.
We've been using the terms cron and crontab as if they were the same, however, they're not. Cron is the Unix program which wakes up every minute and looks for jobs to run. Crontab is the database (table) or listing of jobs to process. In most cases you never interact with the cron command. It is generally set up to run automatically by the system administrator(s). But you can use the crontab command (-r to remove, -e to edit, or -l to list) to set up cron jobs which will be run on a schedule that you set up. The listing of jobs above was generated with crontab -l.
Because cron runs as root, you should fully qualify your path names to any scripts that will be run. For example, you shouldn't use:
Code: | /localpath/daily.sh |
You should use the full path:
Code: | /home/username/cronfiles/daily.sh |
So, to sum up, here is the crontab syntax:
Code: | [minute] [hour] [day of month] [month] [day of week] [command to run] |
If you wish to receive errors by email, add the following line to the top of your crontab:
Code: | MAILTO="your_email_address@example.com" |
Perl commands can be run via Code: | perl -w /path_to_command/perlscript.pl | or if proper steps are taken simply by Code: | /path_to_command/perlscript.pl | If allowed, you can even run php files via the command line using the following syntax Code: | php -C /path_to_command/your_page.php | Not every host allows you to run php files via the command line, so you would need to check to be sure.
Our advice, however, is to put your commands (hourly, daily, weekly, etc.) into a unix shell script instead. By scheduling the shell script instead of individual commands it makes things much easier to manage. In addition to having a cleaner crontab file you can edit the shell scripts and add / remove items without rescheduling the job. |
|