AddThis Social Bookmark Button

Print
O'Reilly Book Excerpts: Running Mac OS X Panther

Scheduling Tasks in Panther

by James Duncan Davidson

Related Reading

Running Mac OS X Panther
Inside Mac OS X's Core
By James Duncan Davidson

Computers are all about automation of tasks, and Mac OS X gives you several tools to help execute tasks at certain times of day and even on a regular and repeating basis. After all, if you want to copy a file from one place to another, or download a set of web pages at 5:25 a.m. on the dot, why should you have to get out of bed when the computer can do it for you?

To perform many of the tasks it has to, the system uses a set of utilities called cron and periodic to manage several housekeeping tasks. This includes such things as tidying up log files and updating system databases like the one used by the locate command (discussed in Chapter 3). This chapter shows you how to use these tools. First, however, you'll learn how to properly set the time on your computer to make sure the tasks you're wanting to automate get triggered on time every time.

Setting the Time

Without having the time accurately set on your machine, it's pretty hard to schedule tasks. You can set the time and date on your computer yourself using the Date & Time preference panel (/Applications/System Preferences). If you spend any amount of time connected to the Internet (and who doesn't these days), you should bypass all that and have your computer set its time from a network time server. Simply click the "Set Date & Time automatically" checkbox in the Date & Time preference panel, as shown in Figure 8-1, and select an appropriate time server near you.

A network time server is nothing more than a machine that has an accurate clock and that understands the Network Time Protocol (NTP), which is designed to keep large numbers of machines synchronized with an accurate clock-; typically one of the atomic clocks that provides the most accurate time possible.

By default, the Date & Time preference panel allows you to set your time against one of three servers provided by Apple. One of these servers is located in the U.S., another in Asia, and the third in Europe. You can also set your computer to synchronize its time with any NTP server you choose—such as a server on your local network that is set up by the network administrators. You can choose to synchronize against a number of publicly accessible time servers on the Internet. A list of network time servers is available at http://www.eecis.udel.edu/~mills/ntp/servers.html.

Figure 8-1. The Date & Time Preference panel
Figure 8-1. The Date & Time Preference panel

When you set a time server, the system does the following:

  • Sets the time server that is being used in /etc/ntp.conf.
  • Makes sure that the ntpd process is running. This process will check the time server periodically and make sure your clock is set correctly.
  • Set the TIMESYNC line in /etc/hostconfig to -YES-, ensuring that ntpd will start when the system is rebooted.

Using iCal to Schedule Tasks

iCal is the personal calendaring application that comes with Mac OS X. iCal features multiple calendars that can be published to other computers and synchronized with .Mac. To schedule something in iCal, you create an event, which is an entry on the calendar that is at a specific time with a specific duration. Events can be one-time occurrences, or they can repeat.

Each event can have an alarm that can display a notice on your computer screen, open a file, or even launch an application at a certain time prior to the event so that it can be ready for you. In addition, alarms can go off even if iCal is not running. iCal uses the iCal Helper application (stored in /Applications/iCal.app/Contents/Resources) to keep track of events and fire them off on schedule whether or not iCal itself is running.

You can use iCal's alarms along with AppleScript to execute just about any kind of task you'd like. To do so requires only three simple steps:

  1. Create an AppleScript application that performs the functionality you want and save it somewhere.
  2. Tip: One logical place to store your scripts is in /Library/Scripts. Anything that you store here will show up in the Script menu, if you've enabled that. To enable the Script menu, go to /Applications/AppleScript and double-click the item Install Script Menu. Here you can store almost anything, including AppleScripts, shell scripts, and even application aliases. (If you're a former Mac OS 9 user, now you've got a replacement for your unconfigurable Apple menu.)
  3. Create a one-time or a repeating event in iCal.
  4. Set the alarm properties on that event to open your AppleScript application.

For example, if you wanted to send a listing of all the files in your Home directory through email every week, you could create an AppleScript with the Script Editor (/Applications/AppleScript) that would do the directory scan and email the results, as shown in Example 8-1.

Example 8-1. An AppleScript to list the files in the Home directory and send them through email

set listing to (do shell script "/bin/ls -l $HOME")
tell application "Mail"
    set the newMessage to (make new outgoing message with properties 
        {subject:"Home dir ls output", content:listing})
    tell newMessage
        make new to recipient with properties {address:"you@somewhere.com"}
    end tell
    send newMessage
end tell

Once you have saved this script as an AppleScript application named ListHomeDir, you can set it up to run in response to an event. The key to running an application when an event is scheduled is to have the alarm set to "Open file" and then select the application as the file to open, as shown in Figure 8-2. Once set, as long as you are logged in to the computer at the time the event is scheduled for, the AppleScript application will be executed. For many tasks, this sort of scheduling works out just fine. But, if you want to run a task every hour, or when you aren't logged in to your Mac, you'll need to go to the command line and use the Unix scheduling tools.

Figure 8-2. Setting a repeating event in iCal to execute a task
Figure 8-2. Setting a repeating event in iCal to execute a task

Using periodic

The periodic tool is designed to organize administrative tasks that need to be performed over and over again at regular intervals. The intervals that periodic supports are: daily, weekly, and monthly. Mac OS X itself has a set of tasks that it runs using the periodic system, including:

  • Tidying up log files and removing scratch files every day
  • Rebuilding the locate database and rotating log files every week
  • Performing log file rotation as well as login accounting every month

The tasks that periodic executes are a set of scripts in the /etc/periodic/daily, /etc/periodic/weekly, and /etc/periodic/monthly directories. To have periodic run your own script, simply add it to one of these directories. For example, if you have a batch of sales reports that you'd like to make a daily snapshot of, you could add the script in Example 8-2 to the /etc/periodic/daily directory.

Example 8-2. A sample periodic script

#!/bin/bash

echo Making daily backup of sales reports

DATE=`/bin/date +%Y-%m-%d`
/bin/mkdir -p /SalesBackups/$DATE
/bin/cp -R /Users/Shared/SalesData/* /SalesBackups/$DATE

periodic also gives you a way to control the order in which scripts run. If you look in the /etc/periodic/daily directory you'll notice that scripts that come with the system start with a number. To have your scripts execute in a particular order, simply prefix them with a number and periodic will take care of ordering their execution, as shown in Example 8-3.

Example 8-3. Listing of the /etc/periodic daily directory


$ ls -l /etc/periodic/daily/
total 24
-r-xr-xr-x  1 root  wheel  1389 30 Aug 20:36 100.clean-logs
-r-xr-xr-x  1 root  wheel  3529 30 Aug 20:36 500.daily

The number in the filename controls the execution order of the scripts. The lower the number, the earlier it will be executed compared to other scripts in the directory. For example, to have the sales backup script in Example 8-2 execute after the rest of the daily tasks, you could save it as /etc/periodic/daily/700.salesbackup.

Viewing the output from periodic

Since periodic runs in the background, any output produced by the scripts is hidden from view. To see what happens, periodic saves the output into the /var/log directory. Daily output is saved to /var/log/daily.out, weekly output is saved to /var/log/weekly.out, and monthly output is saved to /var/log/monthly.out.

cron

The primary tool for scheduling tasks on the command line is the venerable cron. This tool is started automatically by SystemStarter at boot time and runs continuously in the background. Every minute, cron wakes up and consults a set of tables to see if there is anything to be executed at that time, and if so, takes care of executing it. These tables, known as crontab files, are located in two places on the filesystem:

/etc/crontab
The crontab file for the system at large. Each entry in this table represents a command that will be run by the root user and the time that it will be run. Anybody can read this file, but only the root user can edit it.
/var/cron/tabs/
This directory contains the user crontab files for each user on the system who is using cron. These files are hidden and are only visible to the root user, so that other users on the system can't look at each other's crontab files.

Pages: 1, 2

Next Pagearrow