Quick Lesson in Cron
The idea of cron comes from the UNIX world. In UNIX, cron is a daemon that runs in the background and is responsible for all timed events. Although Quartz shares nothing with UNIX cron other than the name and similar expression syntax, it's worth spending a couple of paragraphs to understand the history behind cron. Our goal here is not to confuse UNIX cron expressions and Quartz cron expressions, but you should understand the history behind the Quartz expressions and explore why they look like they do. There is obviously a great deal of intentional similarity.
The UNIX cron daemon wakes up every minute and examines the configuration files, which are called crontabs. (Crontab comes from the phrase CRON TABle, which is a list of jobs and other instructions for the cron daemon.) The daemon inspects the commands that are stored within the crontabs and determines whether any tasks need to be executed.
UNIX Cron Format
You can think of the UNIX crontab as a combination of triggers and jobs because they list both the execution schedule and the command (job) to be executed.
The Cron Expression Format
The crontab format contains six fieldsfive for the schedule and the sixth for the command to execute. (Quartz cron expression has seven fields.) These are the five schedule fields:
- Minute (0059)
- Hour (0023)
- Day (131)
- Month (112)
- Weekday (06 or sunsat)
The UNIX cron format allows for a few special characters in the cron expression, such as the asterisk character (*), which matches all values. Here's an example of a UNIX crontab:
0 8 * * * echo "WAKE UP" 2>$1 /dev/console
This crontab entry prints the string "WAKE UP" to the UNIX device /dev/console every morning at 8 AM. Figure 5.1 shows it in action.
Figure 5.1. The UNIX Cron executing the 0 8 * * * echo "WAKE UP" 2>$1 /dev/console expression