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
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:
- The
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 cronIf 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
| String | Description | Equivalent 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:
- AppleScript: The Definitive Guide, by Matt Neuburg (O'Reilly & Associates, 2003)
- 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
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 22 of 22.
-
10.4 (tiger) can schedule to wake from sleep (most macs)
2005-06-17 12:56:42 day+maclabs [Reply | View]
-
Logged out
2004-03-14 12:50:57 aliscafo [Reply | View]
I was wondering if cron would work if my computer was still turned on but I was logged out. -
Logged out
2004-03-15 11:50:19 James Duncan Davidson |
[Reply | View]
Yes. As long as your computer is on (and not asleep), cron will execute jobs.
-
XJanitor
2004-03-12 04:08:52 alunap [Reply | View]
Nice article, thanks. Someone mentioned anacron. I also have that problem of running scheduled tasks on my powerbook. As an alternative to Anacron, could I recommend XJanitor? I have used it for some months now, and it works really well. It is a single file (a perl script), which you put, eg, in your ~/Library/Scripts folder. Then comment out the three lines calling periodic in /etc/crontab and replace with a single call to XJanitor.pl every 5 minutes. This checks to see whether a scheduled task should have run and hasn't. I use it to do daily backups to my iDisk mirror, knowing it will be properly backed up next time I log on. Could use Backup, but that only allows a single machine to backup to .Mac, and it does it for my desktop machine. This has been mentioned in a comment for another article here on Panther maintenance.
-
G5 Status report while I'm away . . .
2004-03-07 16:35:16 rbannon@mac.com [Reply | View]
I have a G5 at work and I'd like to leave it on 24/7. However, I turn it off each night before I leave and restart in the morning because I am afraid that something may go wrong when I am gone. Is there anyway that I could use a scheduling task to alert me via email (or cell phone) that my system is in trouble? I have the processors saturated at 100% (all the time, but nice-ed) and I am basically afraid that my machine will overheat. No troubles yet, but I hate to have my system go into TURBO FAN mode while I am out of the office -- it would certainly draw attention to the only Mac in the building. email me at rb838 (at) columbia (dot) edu
-
G5 Status report while I'm away . . .
2004-03-07 21:47:18 James Duncan Davidson |
[Reply | View]
Sure, you can have a cron job execute every 5 minutes, do a temperature check, and email you if it's outside of bounds. A quick google search seems to turn up a few utilities that will help you get temperature from the command line. I don't use any of them so I'm unable to give a recommendation.
A better solution would be to stop whatever is that's saturating your processors while you are gone...
-
anacron
2004-03-07 01:38:37 mbelinky [Reply | View]
it might be worth mentioning anacron, which is good for systems that are turned off at night, or notebooks. cron won't execute a task that's been "missed" due to the system not being on. anacron, on the other hand, will check the task's last execution time and run it if needed.
I believe the url is http://anacron.sourceforge.net.
-
anacron
2004-03-07 01:36:56 mbelinky [Reply | View]
it might be worth mentioning anacron, which is good for systems that are turned off at night, or notebooks. cron won't execute a task that's been "missed" due to the system not being on. anacron, on the other hand, will check the task's last execution time and run it if needed.
I believe the url is < href="htp://anacron.sourceforge.net">http://anacron.sourceforge.net.
-
utterly irresponsible
2004-03-06 10:07:17 tempname [Reply | View]
The author is being utterly irresponsible in pointing novice users to the NTP server listing. Public NTP strata-1 and strata-2 timeservers are a precious resource and they don't need to be overwhelmed by home PC users looking for millisecond accuracy on their clocks. There's no need for this anyway- Apple has quite thoughtfully provided NTP servers for those who don't have a company, ISP, or university to provide NTP service, as the author mentions. -
utterly irresponsible
2004-03-06 10:28:52 James Duncan Davidson |
[Reply | View]
I appreciate your concern. However, I do want to point out that the book that this text was excerpted from is not for novices. You can see this in the book description, which starts off with the line Running Mac OS X Panther is the ultimate Swiss Army Knife™ for power users who want to customize, tweak, and generally rev up their Mac.
As well, the page I've referred to contains a lengthy discussion on the rules of engagement for using strata-1 and strata-2 timeservers.
Finally, even though it's not clearly stated in the text, my intention for showing how to change your time server was to allow people to change their machines to point at a time server a local network admin might have set up. (Yes, not all Mac users are home users) I will make sure to state this clearly, as well as to put a warning in about using strata-1 timeservers, into the next revision of the book.
-
I think there's an error here
2004-03-06 08:53:59 macfandave [Reply | View]
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
I believe this would run at 5 IN THE MORNING (A.M.) to run it at 5 PM you'd use:
0 17 * * 1-5 command
Right?
-
I there is an error here
2004-03-06 10:11:33 James Duncan Davidson |
[Reply | View]
Indeed you are right. The crontab uses 24hr time and the text should read 5 a.m.
-
Cronnix
2004-03-06 04:09:03 otto [Reply | View]
If you'd rather use a gui application to edit your cron jobs, check out Cronnix. It's available from http://www.abstracture.de/cronnix/
-
Files in /var/cron/
2004-03-06 02:12:57 2stupid2knowit [Reply | View]
I have 3 file in /var/cron and I can't figure out what they are / do. I have searched Google, but no luck.
sudo ls -al /var/cron
-rw-rw-rw- 1 root wheel 512 Feb 23 13:56 .41A2E0F2-6714-11D8-961EC-67395761B22
-rw-rw-rw- 1 root wheel 512 Feb 23 13:56 .41A4CCB7-6714-11D8-9615-000A95761B22
-rw-rw-rw- 1 root wheel 512 Feb 23 13:56 .D759WtHdZQeVA42GxP4V5aGIAIKh8flgZwzl
drwx------ 3 root wheel 102 Feb 6 13:03 tabs
What are those files with all the numbers? -
Files in /var/cron/
2004-03-06 15:58:50 ambrose [Reply | View]
I don't know what they do, either, but they sure look like Universally Unique Identifiers (UUID's), with a dot in front to make them invisible. Being universally unique, they won't (or better not) show up in Google. Do a 'man uuidgen' to learn more.
-
crontab2english
2004-03-06 01:38:32 larsen [Reply | View]
crontab2english by Sean Burke is a handy tool to cope with crontabs.
-
Something I wanted to do
2004-03-05 20:33:31 r_miller [Reply | View]
Perfect timing. I was just wanting to set up a cron tab for waking my imac when it goes to sleep. However, after reading the article you stated cron will not wake a computer from sleep. Bummer. I have iCal Calling iTunes installed and it will not run most of the time when the computer is asleep either, so I wanted to write a script to wake the computer, What about a simple AppleScript to just start some activity on the computer? Will it run if the computer is asleep. Yeah, I know iCal Calling iTunes is an AppleScript, but I was hoping to use cron or something as a workaround.
-
man for commandline virex
2004-03-05 18:01:22 C.K. Sample III |
[Reply | View]
I like the idea of running virex from the command line, but I cannot locate a man file for it. any pointers?
I don't know if I can decipher all of the usage:
Usage:
uvscan [--afc] [--allole] [--analyse | --analyze]
[-c | --clean] [--cleandocall] [--config file]
[--dam] [-d | --dat | --data-directory] [--delete]
[--exclude file] [-e | --exit-on-error] [--extlist]
[--extensions EXT1[,EXT2...]] [--extra file]
[--fam] [-f | --file file] [--floppya] [--floppyb]
[-h | --help] [--ignore-compressed] [--ignore-links] [--load file]
[--mailbox] [--manalyse | --manalyze | --macro-heuristics]
[--maxfilesize XXX] [--mime] [-m | --move directory]
[--noboot] [--nocomp] [--nodecrypt] [--nodoc] [--noexpire]
[--norename] [--one-file-system]
[--panalyse | --panalyze] [-p | --atime-preserve | --plad]
[--program] [-r | --recursive | --sub]
[--secure] [-s | --selected] [--summary]
[-u | --unzip] [-v | --verbose] [--version] [--virus-list]
[-] {file / directory}
-
man for commandline virex
2004-03-08 08:26:52 mgresko [Reply | View]
/usr/local/vscanx/vscanx /var -r -v --summary -c --exclude /usr/local/vscanx/excludefd is what i use.
/usr/local/vscanx/vscanx is the command
/ is where i am starting my scan from
-r -v --summary means scans directories beneath the root directory / and be verbose and i want a summary at the end
-c means clean the infected files
--exclude /usr/local/vscanx/excludefd is any files you want to exclude
if you want to only scan the var folder and not the folders beneath it and wanted to scan every single file in the var folder you would use /usr/local/vscanx/vscanx /var -v --summary -c
HTH -
There should be a PDF manual
2004-03-06 01:11:38 FJ de Kermadec |
[Reply | View]
Hi !
There should be a PDF manual inside of the "Virex" folder (located in your Applications folder) : this file contains a more user-friendly explanation of how vscanx works from the command line.
F.J.






http://www.chem.duke.edu/compteam/mac/waking-tiger.html
There is a new feature in 10.4 (tiger): the "Energy Saver" can now be scheduled to wake most macs from sleep. In eariler versions like 10.2 (jaguar) and 10.3 (panther), you could schedule a power-up at a recurring time (like 3am every day), but that would NOT wake a box that was powered-on but sleeping. Now, in 10.4, with the ability to schedule a power-on or wake-up, with judicious choice of settings, you can pretty much guarantee certain certain scheduled tasks always are run, such as the 3 periodic task groups (daily, weekly, and monthly).
For example, suppose I want to ensure that these 3 periodic task-groups always run. I think the following should work (I have yet to verify):
1. In Energy-Saver, "Put the computer to sleep when it is inactive for:" 3 hrs (not next highest value, "Never").
2. In Schedule, set "Start up or wake: Every Day: at 3:05am". The daily periodic tasks start at 3:15am by default.
3. I think the periodic-weekly should also be editted to run at 4:30 (as it did in Panther), also discussed at URL below.
4. Re-schedule the periodic-monthly to run at 4:45am instead of 5:30am (Tiger uses /System/Library/LaunchDaemons (instead of /etc/crontab) so you must edit com.apple.periodic-monthly.plist as shown at URL below:
http://www.chem.duke.edu/compteam/mac/waking-tiger.html