An Introduction to Tiger Terminal, Part 5

In previous parts of this Tiger Terminal series (Part 1, Part 2, Part 3, & Part 4), I introduced the OS 10.4 version of the terminal.app, shell basics, CLI network utilities, and shell scripting. In Part 5, I'll show you how Tiger Mac OS X runs regularly scheduled commands and scripts to execute recurring jobs, like system maintenance and backups by comparing the "old" way, using the Unix tool called cron (for chronological), with the new Tiger method of using the launchd daemon.

A daemon is a background process or program that runs unattended. It's usually committed to perform systemwide functions. cron is a Unix daemon that enables users to execute scripts or commands automatically at a specified day and time. With Tiger OS X 10.4, Apple introduced a new tool called launchd that is intended to replace cron and, as we'll soon see, many other traditional Unix-style services. To best explain, I'll look at cron because it's so ubiquitous in the Unix/Linux world and then I'll discuss launchd, including how it works, how to use it and what it may mean for the future. But first . . .

Why launchd?

There are many processes and subsystems involved in booting a traditional Unix-style operating system (including previous versions of OS X), in launching applications at specific times, and in launching on demand: init, mach_init, (x)inetd, cron, rc, watchdog, at, startup items, and login items.

The first process on a Unix system is init but its role varies between different systems: on Linux boxes, init maintains system run levels and launches services on demand; on BSD systems, init runs startup scripts and periodically launches getty (which reads and invokes login). In previous versions of OS X, mach_init would run core system services (see Figure 1); StartupItems, init.d, or rc/rc.local would start boot time services; xinetd started network services; login items would come from a user's .profile or .xinitrc or from StartupItems; and cron would run scheduled services. These are all still available in Tiger but the intention is clearly to move away from using the "old" methods for new services and transfer them to launchd instead.

If you were to run the ps command (get the process status of all users; man ps) on a Panther system, you would see that the first two PIDs (Process IDs) are init and mach_init (although mach_init has a PID of 2, it is started first on the system and init is started second). Run the ps command on a Tiger system, and you'd see that launchd has replaced both these processes and has a PID of 1.

Figure 1. Panther ps ax vs. Tiger ps ax

With Tiger, Apple took a daring approach by deciding to supersede and eventually replace these and other services with one system startup daemon, launchd. By providing a unified interface for managing programs started by the system, launchd can do all the jobs that the myriad Unix programs described above can: launchd can manage daemons for the entire system or for a single user; it can launch daemons on demand and monitor them. It can, therefore, speed up the boot process and provide efficient administration for managing services.

The Old . . .

As an example of how scheduling maintenance tasks have changed in Tiger, let's concentrate on one of the daemons, Vixie cron (the version of cron in OS X), how it functioned in Panther, OS X 10.3, and its role in Tiger, 10.4.

In Panther, cron is started at boot by SystemStarter and runs continuously in the background (it's a daemon, remember?). cron checks tables (called crontab files) in two directories for new instructions or tasks that need to be executed. One directory is /var/cron/tabs/ which contains the user crontab files (visible only by root). The other is the one we're interested in: /etc/crontab. This is the crontab file for the entire system (anybody can view this file but only root can edit it). Figure 2 is a crontab for Panther. To open this file, you'll need to cd to the /etc directory and then type sudo pico crontab at the command prompt (remember, you have to be running OS X 10.3 to get this, and remember pico is the basic default editor in Panther).

Figure 2. Panther crontab

This should look pretty familiar to you if you've been following along with Parts 1 - 4 of this series! The top line is a comment line indicating the location and name of the file. The next few lines are our shell and path descriptions. The next set of commented lines are read left to right and represent minute, hour, day of the month (mday), month, and weekday (wday) in numerical format. The asterisk represents "every." The user of the job (which tells cron to run it as if root were running it) and the task to be run (periodic daily, periodic weekly or periodic monthly) completes the line. So the periodic daily task will run at 3:15 a.m. on every day of the month, every month, and every day of the week.