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.
Here are some tips, tricks, and common pitfalls of
- Unlike the shell you use in a terminal window or for a shell script,
crondoes not expand environment variables in-place in crontab files. In other words, if you place the lines:
in a crontab, PATH is not set to what you expect. Instead, you must expand all variables manually, as in these lines:
However, because each cron command is executed by a shell, a command can refer to variable names. For example, if you place this command:
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
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
uptimeoverwrites the file named daily (
>daily) and is appended to the file weekly (
- Use the access control lists (ACLs)—/etc/cron.allow and /etc/cron.deny—to
permit or preclude individual users from running
cronjobs. If you want to greatly limit access to
cronto 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:
any user other than Joe and Zelda is refused access to
% 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
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
croncarefully. Paths can differ, as can features and conveniences. To read about crontab file syntax, type
man 5 crontabat the command line. To read about the crontab utility, type
man 1 crontab. To find the options available for the
crondaemon itself, type
man 8 cronat the command line.
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:
cronand 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
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
cronis copying files from a master to its many slaves.
Rsyncis a modern utility that distributes and synchronizes collections of files across multiple systems. Many Web masters combine
rsyncto 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,
rsyncexactly copies (
-avz) /var/www/site to /var/www on slave1.
Use the command-line
|) 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
dfis mailed weekly to users Andy and Bob to monitor disk usage.