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.

Many Different Versions of UNIX Cron

You'll find different versions of cron, each with slightly different features. We're interested only in comparing against the Quartz CronTrigger, so we generalize the various UNIX versions of cron for our discussion.

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:

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

Категории