Testimonials
My working experience with Grossi Web was a very good one. I work with a lot of web development teams that have an easy time talking about what you want but not being able to pull it off. That was not the case with Grossi Web. There was good communication throughout the process and they were able to pull off what they promised. -- Mike Lewis :: Modern Hook |
Using Cron Job to automate a task or script
Cron is a Unix scheduling utility. The syntax is a bit cryptic, but it can be figured out. You'll need to either have telnet (shell) access to your server, or some other way to interface with it (like 34SP's cron front-end). You'll also have to have a host which allows you to run cron jobs.
For example, I have the following two entries in my crontab (cron table):
42 2 * * 0 /usr/bin/zip -q access-log.`/bin/date +\%m\%d`.zip access-log
59 23 * * * /home/server_name/other_path/batch/snapshot.sh
Reading from left to right, the first few symbols (numbers or *) refer to when a job will run.
For example, the second entry in my crontab 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 my 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:
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.
I'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 or listing of jobs to consider. I don't own my host, so I don't have access to run cron. But I 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 I 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:
/batch/snapshot.sh
You should use the full path:
/home/path/batch/snapshot.sh
So, to sum up, here is the crontab syntax:
[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: