macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Scheduling Tasks in Panther
Pages: 1, 2

The System crontab File

Example 8-4 shows the system crontab as it appears in a default installation. While this file is for system tasks, you should always use the crontab file for your user.

Example 8-4. The system crontab file

# /etc/crontab
SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
#
#minute hour    mday    month   wday    who     command
#
#*/5    *       *       *       *       root    /usr/libexec/atrun
#
# Run daily/weekly/monthly jobs.
15      3       *       *       *       root    periodic daily
30      4       *       *       6       root    periodic weekly
30      5       1       *       *       root    periodic monthly

The crontab file format is similar to that of many other Unix utilities. Any line beginning with the hash character (#) is a comment. The first three non-commented lines of the file set the environment that cron will run with. The remaining lines of the crontab file consist of five numbers defining the time pattern at which a particular task is to run. The end of the line contains the command to run. In the case of the system crontab, the command also contains the name of the user under which to run the command. As the file itself indicates, each of the five numbers corresponds to a different time interval, arranged in order of finer to larger granularity. Figure 8-3 describes the settings for each of these fields. In addition to numbers, each field can contain an asterisk (*) character, which means match every possibility for that field.

To interpret the lines in the crontab file in Example 8-4, read the fields for each line from left to right. For example, the first field (after the comments) in the system crontab indicates that periodic daily should be run on the fifteenth minute of the third hour on any day of the month on any given month on any day of the week. This means that periodic daily will run at 3:15 a.m. every day. The second line indicates that the periodic weekly command will run on the thirtieth minute of the fourth hour of any Sunday; that is, it runs every Sunday at 4:30 a.m.

For each of the fields, you can also specify a list or range of numbers. For example, if you wanted to run a command every 15 minutes, you could use the following line:

0,15,30,60 * * * * command

Figure 8-3. The crontab file format
Figure 8-3. The crontab file format

Or, if you wanted a command to only run at 5:00 p.m. on weekdays, you could use the following:

0 5 * * 1-5 command

The User crontab

To set up tasks that will get executed, you have to edit your own personal crontab. You can take a look at what you already have in your crontab file by using the crontab command:

$ crontab -l
crontab: no crontab for duncan

This output means nothing has been scheduled yet. By default, a user account won't have a crontab when it is first set up.

Editing a user crontab

There are two ways to edit your crontab. The first involves using whatever editor you've set up on the command line (for example, vi, Emacs, and pico). The second involves using any editor you want (such as TextEdit or BBEdit) and loading a text file as your crontab. To edit your file on the command line, use the following command:

$ crontab -e

For your first crontab entry, let's add a line that will make your computer say "hello" every minute. To do this, add the following line to your crontab file:

* * * * * osascript -e 'say "hello"'
Tip: If you get stuck in an editor that you are unfamiliar with, remember that you can get out of vi by typing :q! and out of Emacs by typing Control-X then Control-C. See Chapter 3, The Terminal and Shell, for more info about command-line editors.

Now, every minute of every day that your machine is on, it will say "hello" to you, which could become annoying indeed. There are a couple things going on here:

  1. The osascript -e 'say "hello"' command is being issued by your system every minute, based on the five preceding asterisks.
  2. The command uses the default system voice set in the Speech preference panel to speak the word "hello" on cue.

But now that you've got a crontab file installed, you can use crontab to list the file:

$ crontab -l
* * * * * osascript -e 'say "hello"'

The other way to create a crontab file is to use an editor like TextEdit or BBEdit. To get the current crontab out in a form that you can open with any editor, save the file on your hard drive, and then execute the crontab command as follows:

$ crontab mycrontabfile
Tip: This also gives a way to quickly reset the crontab file for a user. By passing the /dev/null file into crontab, the user's crontab will be set to an empty file.
Warning: Using the crontab comment to specify a file is also a good way to accidently lose any cron settings that you have in place. Be sure to check your crontab before loading in a new crontab file.

To retrieve your crontab for editing, you can direct the output using the following command:

$ crontab -l > mycrontabfile

Running Virex from cron

If you've installed the McAfee Virex virus scanner from .Mac, you have the vscanx command-line virus scanner installed at /usr/local/vscanx/vscanx . You can take advantage of this and have your disk scanned for viruses from cron instead of from the GUI. This is a bonus that the virus scanner will run no matter who's logged in to the computer or even if nobody is.

To enable this, delete the .VirexLogin item from list your Startup Items in the Accounts preference panel. Then add a line to your crontab . le to execute /usr/local/vscanx/vscanxwhenever you'd like.

Note that you need to give the full path to vscanx since it isn't installed in one of the standard binary directories, such as /usr/bin.

Additional configuration settings

The cron command on Mac OS X has been enhanced compared to those found on some other Unix variants. For example, you can use the following more readable entries in the time field:

  • Days of the week can be indicated by their abbreviated name: sun, mon, tue, wed, thu, fri, sat.
  • Months can be indicated by their abbreviated name: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec.
  • You can indicate step values by using a fraction notation such as 8-17/2 which, if it were in the hours field, would mean "every two hours between the hours of 8 a.m. and 5 p.m."

Some special strings can be used in crontab files. Table 8-1 has a list of these strings. Except for the last one, all these strings replace the time fields. The last, @AppleNotOnBattery, can be used in front of a command to prevent it from running when your laptop is disconnected from AC power. This ensures that you don't run disk-intensive tasks when you need your battery the most. For example, if you write a script that copies all your files from your ~/Documents folder to some safe storage location that you want to run only when your PowerBook is plugged in, you would use the following crontab entry:

0 * * * * @AppleNotOnBattery ~/bin/copyfiles

Table 8-1. Special strings that can be used in a crontab

StringDescriptionEquivalent To
@ reboot Run when the system reboots 
@ yearly Run on midnight of January 1 0 0 1 1 *
@ monthly Run at midnight on the first of the month 0 0 1 *
@ weekly Run at midnight each Sunday 0 0 * * 0
@ daily Run every day at midnight 0 0 * * *
@ hourly Run every hour at the top of the hour 0 * * * *
@ AppleNotOnBattery Prevents command from running if the system is on battery 

What About at, batch, atq, and atrm?

If you're coming to Mac OS X from another Unix system, you may be familiar with using the at, batch, atq, or atrmcommands for scheduling tasks. These commands exist in Mac OS X, but they have been disabled by Apple due to power management concerns. If you really need these commands, you must enable the atruncommand in /etc/crontab, but do so at your own risk.

Sleep and cron

The cron system won't execute while your system is asleep. This is because the CPU is powered down and there's just enough happening in your machine to keep the contents of memory ready when you want to wake the system up.

Sometimes this isn't a big deal. For example, if you use a crontab line to remind you to stretch every hour, then you won't mind it not running. However, for other tasks that you would like to have run, it can create a bit of a problem. The best piece of advice is to time tasks that need to be run when your system is less likely to be in sleep mode.

Changing periodic's Execution Time

By default, periodic runs daily tasks at 3:15 a.m., weekly tasks at 4:30 a.m., and monthly tasks at 5:30 a.m., because these are the hours when your system should be idle while you are sound asleep. If your system isn't on 24 hours a day, you might consider changing the times that these tasks run to a time when your Mac will be powered on and somewhat idle. For example, if you wanted the daily tasks to run during your lunch hour, the weekly tasks on Monday at 10:00 a.m. while you're in a meeting, and the monthly tasks at 10:30 a.m., you would edit the /etc/crontab file to match Example 8-5. Since the system crontab doesn't belong to a user, you can't use crontab -e. Instead you must edit the file directly.

Example 8-5. The system crontab with the periodic tasks set at a more reasonable time

15      12       *       *       *       root    periodic daily
0       10       *       *       2       root    periodic weekly
30      10       1       *       *       root    periodic monthly

Further Explorations

For more information about the technologies in this chapter, see the following resources:

You may also be interested in consulting the following manpages:
  • periodic
  • cron
  • crontab

James Duncan Davidson is a freelance author, software developer, and consultant focusing on Mac OS X, Java, XML, and open source technologies. He currently resides in San Francisco, California.


Return to the Mac DevCenter