Customizing timetables
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
| Position | Field | Values | Notes |
|---|---|---|---|
| 1 | Minute | 0-59 | |
| 2 | Hour | 0-23 | |
| 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 jan
or may.
|
| 5 | Day of week | 0-7 | Both 0 and 7
refer to Sunday. You can also use the first three letters of each day's name,
such as mon or wed.
|
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 cron
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
| Schedule | |||||
|---|---|---|---|---|---|
| Minute | Hour | Day of month | Month | Day of week | Description |
0 | 1 | 15 | 1,3,5,7,9,11 | * | 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
command 0 1 15 jan,mar,may,jul,sep,nov *. When
specifying a list, avoid putting a space after a comma.
|
0-59/15 | * | * | * | * | This schedule runs the command every 15 minutes. |
30 | * | * | * | wed,fri | 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.) |
0,30 | 0-5,17-23 | * | * | * | 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. |
0 | 0 | 1 | 1 | * | Run the command once per year at the stroke of midnight of 1 January. |
0 | 0 | * | * | 0 | Run the command at the stroke of midnight every Sunday. This is the equivalent of once per week. |
30 | 0 | 10,20,30 | * | 6 | 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 cron
provides shorthand for common schedules. Table 3
lists some of the choices.
Table 3. Helpful shorthand for common schedules
| Shorthand | Description |
|---|---|
@reboot | Run the command whenever the machine reboots. |
@daily | A shorthand for once per day. |
@weekly | A shorthand for once per week. |
@annually | A shorthand for once per year. You can also write this as
@yearly.
|
@midnight | Run the command once per day at midnight. A synonym for this shorthand is
@daily.
|
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 |
Submit the personal crontab to the crontab utility
After editing your file—say, ~/mycrontab—submit it to
cron with the aptly named
crontab utility:
% crontab ~/mycrontab |
To see what's stored in cron, type
crontab -l:
% 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 crontab -r:
% whoami joe % crontab ~/mycrontab % crontab -l PATH=/usr/bin:/bin:/usr/local/bin ... % crontab -r % crontab -l crontab: no crontab for joe |




