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
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
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 -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:
osascript -e 'say "hello"'command is being issued by your system every minute, based on the five preceding asterisks.
- 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,
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
0 * * * * @AppleNotOnBattery ~/bin/copyfiles
Table 8-1. Special strings that can be used in a crontab
|@ reboot||Run when the system reboots|
|@ yearly||Run on midnight of January 1|
|@ monthly||Run at midnight on the first of the month|
|@ weekly||Run at midnight each Sunday|
|@ daily||Run every day at midnight|
|@ hourly||Run every hour at the top of the hour|
|@ 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
For more information about the technologies in this chapter, see the following resources:
- AppleScript: The Definitive Guide, by Matt Neuburg (O'Reilly & Associates, 2003)
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
10.4 (tiger) can schedule to wake from sleep (most macs)
2005-06-17 12:56:42 day+maclabs [View]
- Trackback from http://steven.vorefamily.net/2004/05/25.html#a1955
Scheduling Tasks in Panther
2004-05-25 10:55:17 [View]
- Trackback from http://ccs.usask.ca/macblog/archives/000107.html
Scheduling Tasks in Panther, making time for you.
2004-05-12 12:40:48 [View]
- Trackback from http://www.jayallen.org/journey/2004/03/scheduling_tasks_in_panther
Scheduling tasks in Panther
2004-03-21 00:25:32 [View]
2004-03-14 12:50:57 aliscafo [View]
2004-03-12 04:08:52 alunap [View]
2004-03-07 16:35:16 email@example.com [View]
2004-03-07 01:38:37 mbelinky [View]
2004-03-07 01:36:56 mbelinky [View]
2004-03-06 10:07:17 tempname [View]
2004-03-06 08:53:59 macfandave [View]
2004-03-06 04:09:03 otto [View]
2004-03-12 15:58:18 jamesreynolds [View]
2004-03-06 02:12:57 2stupid2knowit [View]
Files in /var/cron/
2004-03-06 15:58:50 ambrose [View]
Files in /var/cron/
2004-03-06 18:06:00 2stupid2knowit [View]
2004-03-06 01:38:32 larsen [View]
2004-03-05 20:33:31 r_miller [View]
2004-03-05 18:01:22 C.K. Sample III | [View]
man for commandline virex
2004-03-08 08:26:52 mgresko [View]
There should be a PDF manual
2004-03-06 01:11:38 FJ de Kermadec | [View]