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:  18803 views
Comments:  

Using cron to your advantage

Get a few tips and tricks for using cron, and discover why this daemon and others like it may just be your best friends.

Tips and tricks

Here are some tips, tricks, and common pitfalls of cron:

  • Unlike the shell you use in a terminal window or for a shell script, cron does not expand environment variables in-place in crontab files. In other words, if you place the lines:
    HOME=/home/joe
    PATH=$HOME/bin:/usr/bin:/bin
    

    in a crontab, PATH is not set to what you expect. Instead, you must expand all variables manually, as in these lines:

    HOME=/home/joe
    PATH=/home/joe/bin:/usr/bin:/bin
    

    However, because each cron command is executed by a shell, a command can refer to variable names. For example, if you place this command:

    @daily  $HOME/bin/cleanup_daily.sh 
    

    in your personal crontab (notice that the user name parameter is omitted in the line above), $HOME is expanded properly.

  • Do not schedule compute-intensive tasks to begin at the same time, such as @midnight. If possible, start such tasks independently throughout the wee hours of the morning, avoiding competition for resources.
  • As mentioned above, the environment variable SHELL is set to /bin/sh by default. If left unchanged, all commands in the crontab are interpreted by /bin/sh. However, if you aren't familiar with /bin/sh and prefer another shell, you can set SHELL and use your shell's syntax for commands.

    For example, if you set SHELL=/bin/zsh, all commands can use the facilities of the Z shell, such as its advanced redirection operators:

    SHELL=/bin/zsh
    @daily  uptime > daily >> weekly 
    

    Here, the output of the command uptime overwrites the file named daily (>daily) and is appended to the file weekly (>> weekly).

  • Use the access control lists (ACLs)—/etc/cron.allow and /etc/cron.deny—to permit or preclude individual users from running cron jobs. If you want to greatly limit access to cron to just a few users, list those users' names in /etc/cron.allow. Anyone not named cannot submit a crontab using the crontab utility. However, if you want to grant wide access and prohibit just a handful of users from access to cron, list the restricted users in /etc/cron.deny.

    For example, if /etc/cron.allow looks like this:

    joe
    zelda
    

    any user other than Joe and Zelda is refused access to cron:

    % whoami
    strike
    % crontab ~/.crontab
    You (strike) are not allowed to use this program (crontab)
    See crontab(1) for more information
    

  • To disable email reports from cron, put MAILTO="" in the crontab.
  • Again, do not use spaces in lists. Separate list values with commas. In Vixie cron, do not use day names and month names in ranges.
  • Read the documentation for your system's cron carefully. Paths can differ, as can features and conveniences. To read about crontab file syntax, type man 5 crontab at the command line. To read about the crontab utility, type man 1 crontab. To find the options available for the cron daemon itself, type man cron or man 8 cron at the command line.

The systems administrator's best friend

Cron and its ilk are invaluable for systems administration. If you find yourself performing the same task over and over again, consider automation with cron. Shell scripts are often necessary to capture complex tasks with many steps, but you can also achieve a great deal with a single command line run every so often.

Here are just a handful of ideas:

  • Combine cron and your favorite database tools to create daily dumps. For example, the command
    @daily joe mysqldump -pjoespwd accounts > $HOME/backups/accounts.`date +%F`.sql
    

    dumps the database named accounts daily to a file. The embedded date command (`date +%F`) ensures that the file name is unique, as in accounts.2008-08-07.sql. The command runs as joe, so Joe's password is specified with -p. This command could also simply appear in Joe's own crontab, because his MySQL credentials are required for the dump.

  • The locate subsystem indexes all files on the system and stores the full path to each file in a database. You can then query the database from the command line to instantly find files. Of course, you can search for files on demand with find, but you must wait as it crawls the file system each time it runs.

    To make locate effective, though, you must index the file system on a regular basis, because files come and go all the time. This is a perfect application for cron.

    0 0,12 * * * root updatedb
    

    This crontab entry runs updatedb, the locate update utility, twice per day.

  • Another obvious task well suited to automation with cron is copying files from a master to its many slaves. Rsync is a modern utility that distributes and synchronizes collections of files across multiple systems. Many Web masters combine cron and rsync to push a master copy of the Web site to each server in the farm.
    @midnight www rsync -avz /var/www/site slave1:/var/www
    

    At midnight each night, rsync exactly copies (-avz) /var/www/site to /var/www on slave1.

    Use the command-line mail utility and the shell pipe operator (|) to send the output of a task to one or more people on your staff.

    @weekly root df --print-type --local -h |& mail -s "Weekly df report" andy bob 
    

    Here, the output of df is mailed weekly to users Andy and Bob to monitor disk usage.

5 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