A cron timetable allows you to run jobs every minute or at a specific minute on a specific day. The parameters for scheduling are very flexible.
There are five fields—like "knobs"—that you can tweak to adjust frequency: minute, hour, day of month, month, and day of week. Table 1 summarizes what you can tune in each field.
Table 1. Scheduling options for cron jobs
|3||Day of month||1-31||Unlike minute and hour, day of month is not zero based.|
|4||Month||1-12||Month is not zero based, either. Additionally, instead of 1-12, you can use the first
three letters of each month's name, such as |
|5||Day of week||0-7||Both |
In addition to a name or number, you can use the asterisk
*) to specify "every." For instance, an asterisk
in the minute position indicates every minute of the day. (There are certainly
valid uses for such a high frequency, but be careful that such tasks are very
lightweight and do not run for protracted periods.)
You can also use lists of values, ranges, and steps (increments) to specify many, an inclusive range of values, and an alternating range of values, respectively. You can even combine lists and ranges. A list is a comma-separated set of values. A range is a starting and an ending value, inclusive, and an optional step value.
Let's look at some examples. Each row in Table 2 contains a
timetable and a description. A command is executed by
when the minute, hour, and month of year fields match the current time and if
both the day of month and day of week are restricted (that is, not
*) when at least one of the two fields matches
the current time.
Table 2. Example timetables for cron jobs
|Minute||Hour||Day of month||Month||Day of week||Description|
|Run the command at 1 a.m. on the 15th of January, March, May, July,
September, and November. For better legibility, you could have written the
|This schedule runs the command every 15 minutes.|
|This timetable executes its command every hour, on the half-hour, on Wednesdays and Fridays only. (You can use day names and month names in lists but not in a range.)|
|Run the command on the hour and half-hour from midnight to 5 a.m. and again between 7 p.m. and 11 p.m.|
|Run the command once per year at the stroke of midnight of 1 January.|
|Run the command at the stroke of midnight every Sunday. This is the equivalent of once per week.|
|Because the day of the month and day of the week are restricted, this command runs at 12:30 a.m. every Saturday and on the 10th, 20th, and 30th day of every month, except February.|
As you can see, you can express virtually any schedule using the five parameters
provided. As an added convenience, Vixie
provides shorthand for common schedules. Table 3
lists some of the choices.
Table 3. Helpful shorthand for common schedules
|Run the command whenever the machine reboots.|
|A shorthand for once per day.|
|A shorthand for once per week.|
|A shorthand for once per year. You can also write this as
|Run the command once per day at midnight. A synonym for this shorthand is
If you prefer to use the shorthand, replace the first five fields of the
cron command with the moniker. The following
command, albeit contrived, is much simpler to get at a glance.
@daily root /usr/local/scripts/clean_old_files.sh
With the basics under your belt, let's look at some sample user crontab commands. You can certainly apply the same commands system-wide: Just remember to specify a user name after the day of week field (the fifth field) in all system crontab entries.
To use a personal crontab, create a file for your commands with any text editor. By convention, personal crontab files are kept in ~/.crontab, but you can name the file anything you like.
PATH=/usr/bin:/bin:/usr/local/bin # # Every day, print and delete all temporary files whose names begin with '.#' @daily find $HOME -type f -name '.#*' -print -delete # # Every week, show me what is consuming space in my home directory @weekly du -sh $HOME
After editing your file—say, ~/mycrontab—submit it to
cron with the aptly named
% crontab ~/mycrontab
To see what's stored in
% crontab -l PATH=/usr/bin:/bin:/usr/local/bin # # Every day, print and delete all temporary files whose names begin with '.#' @daily find $HOME -type f -name '.#*' -print -delete # # Every week, show me what is consuming space in my home directory @weekly du -sh $HOME
You can use the
crontab utility to replace your
crontab at any time. Simply submit a new file or a revision of the same
file. To remove your crontab jobs, type
% whoami joe % crontab ~/mycrontab % crontab -l PATH=/usr/bin:/bin:/usr/local/bin ... % crontab -r % crontab -l crontab: no crontab for joe