Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Using cron to automate maintenance

The cron subsystem schedules tasks to run any hour of the day or night, making regular upkeep a breeze

Martin Streicher (martin.streicher@gmail.com), Chief Technology Officer, McClatchy Interactive
Photo of Martin Streicher
Martin Streicher is a freelance Ruby on Rails developer and the former Editor-in-Chief of Linux Magazine. Martin holds a Masters of Science degree in computer science from Purdue University and has programmed UNIX-like systems since 1986. He collects art and toys. You can reach Martin at martin.streicher@gmail.com.
(An IBM developerWorks Contributing Author)

Summary:  To leverage round-the-clock computing, tasks must run at all hours of the day. You could punctuate your sleep with waking interludes to log in and run this command or that command on dozens of machines, or you can enjoy your forty winks and turn the work over to the ubiquitous cron, a daemon, or perennial process, to execute commands on a schedule. From very often to every so often, cron happily minds the clock and runs jobs day or night. Learn how to configure and maintain cron, and discover just some of its many uses.

Date:  07 Oct 2008
Level:  Intermediate PDF:  A4 and Letter (69 KB)Get Adobe® Reader®

Activity:  18859 views
Comments:  

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.

Cron fields

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
PositionFieldValuesNotes
1Minute0-59
2Hour0-23
3Day of month1-31Unlike minute and hour, day of month is not zero based.
4Month1-12Month 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.
5Day of week0-7Both 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
MinuteHourDay of monthMonthDay of weekDescription
01151,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,friThis 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,300-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.
0011*Run the command once per year at the stroke of midnight of 1 January.
00**0Run the command at the stroke of midnight every Sunday. This is the equivalent of once per week.
30010,20,30*6Because 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
ShorthandDescription
@rebootRun the command whenever the machine reboots.
@dailyA shorthand for once per day.
@weeklyA shorthand for once per week.
@annuallyA shorthand for once per year. You can also write this as @yearly.
@midnightRun 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


Sample crontab commands

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.

Create a personal crontab

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

View what's stored in cron

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

Replace your crontab

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

3 of 8 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX
ArticleID=342930
TutorialTitle=Using cron to automate maintenance
publish-date=10072008
author1-email=martin.streicher@gmail.com
author1-email-cc=mmccrary@us.ibm.com