< Previous PageNext Page > Hide TOC

Daemons

By the time a user logs in to a Mac OS X system, a number of processes are already running. Many of these processes are known as daemons. A daemon is a background process that provides a service to the users of the system. For example, the cupsd daemon coordinates printing requests while the httpd daemon responds to requests for web pages.

Most daemons run in the root context of the system—that is, they run at the lowest level of the system and make their services available to all user sessions. Daemons at this level continue running even when no users are logged into the system. Because of this fact, the daemon program should have no direct knowledge of users. Instead, the daemon must wait for a user program to contact it and make a request. As part of that request, the user program usually tells the daemon how to return any results.

Note: For more information about the root context and user sessions, see Multiple User Environments.

Contents:

Communicating With Daemons
Viewing the Currently Running Daemons
Running a Process on a Schedule


Communicating With Daemons

Applications communicate transparently with built-in system daemons most of the time. For example, when you use the CFNetwork API to open a network connection, the API itself may communicate with several daemons to process your request. For the most part, your program does not need to worry about the details of this communication.

Note: It is possible to communicate with many system daemons directly if needed. Most system daemons are part of the Darwin layer of Mac OS X and have well-documented communication protocols. In most cases, though, it is easier, more practical, and more reliable to let the Mac OS X libraries and frameworks handle this communication for you.

When creating a custom daemon, however, you must define the protocol for receiving requests and returning the results. To simplify matters for clients who might use your daemon, you should define your own library of routines for initiating requests and receiving results.

For example, if you created a daemon to manage database transactions, you would likely also create a library of functions for communicating with that daemon. Client applications linking to your library would then talk transparently to your daemon without the need to understand the communication mechanism itself.

There are three major classes of communication mechanisms commonly used between daemons and their clients: traditional client-server communications (including Apple events, TCP/IP, UDP, other socket and pipe mechanisms, and Mach IPC), remote procedure calls (including Mach RPC, Sun RPC, and Distributed Objects), and memory mapping (used underneath the Core Graphics APIs, among others).

In general, you should use a traditional client-server communication API. Code based on these APIs tends to be easier to understand and maintain than RPC or memory mapping designs. It also tends to be more portable to other platforms.

If both your client and daemon are written in Cocoa, and if most of your communication involves sending a message and expecting a reply, you should consider Distributed Objects, which is an RPC mechanism.

Because memory mapping requires more complex management (and represents a security risk if you are not careful about what memory pages you share), you should use memory mapping only if your client and daemon require a large amount of shared state with low latency, such as passing audio or photo data in a real-time fashion.

The details of using these communication mechanisms are outside the scope of this document, but you can find documentation elsewhere in the ADC Reference Library to help you with the details. Some related documents include:

Viewing the Currently Running Daemons

If you want to see the daemons currently running on your system, use the Activity Monitor application (located in /Applications/Utilities). This application lets you view information about all processes including their resource usage. Figure 1 shows the Activity Monitor window and the process information.

Note: If you want to know more about the services provided by a particular daemon, consult the man page for that daemon. You can also view the manual pages online by reading Mac OS X Man Pages.


Figure 1  Processes shown in Activity Monitor

Processes shown in Activity Monitor

Running a Process on a Schedule

Sometimes you need to run a daemon or other background process on a timed schedule. In Mac OS X, you can do this in four ways: cron jobs, at jobs, launchd timed jobs, and periodic jobs. This section explains these methods briefly and provides links to manual pages that provide additional details.

Timed Jobs Using Periodic Jobs

If your timed job just needs to be run periodically, the easiest way to make this happen is to create a periodic job. To create a periodic job, you simply create a shell script that executes your program with the desired flags and place it in the daily, weekly, or monthly directory in /etc/periodic.

Mac OS X runs these jobs in different ways depending on the version of Mac OS X. In Mac OS X v10.3 and earlier, cron is responsible for starting periodic jobs. In v10.4 and later, launchd starts the jobs.

As a result, if your computer is asleep at the scheduled time, in Mac OS X v10.3 and earlier, the job does not run; in Mac OS X v10.4 and later, the job executes automatically when the computer wakes up. For this reason, you should not assume that a job will run at a particular time or on a particular day.

Note: If the computer is turned off at the scheduled time, the jobs does not run at all, regardless of what version of Mac OS X you are using.

You can configure the periodic tool by modifying /etc/defaults/periodic.conf. For more information, see the manual page for periodic.conf.

Timed Jobs Using cron

The cron daemon is the most common tool for executing a job at a particular time. Although all versions of Mac OS X support cron jobs, beginning in v10.4, the functionality of cron is largely superseded by launchd, and all periodic jobs built into Mac OS X are launchd jobs.

A cron job is not guaranteed to run. If the system is turned off or asleep at the designated time, the job does not execute until the designated time occurs again.

Systemwide cron jobs can be installed by modifying /etc/crontab. Per-user cron jobs can be installed using the crontab tool. The format of these crontab files is described in the man page for the crontab file format.

Because installing cron jobs requires modifying a shared resource (the crontab file), you should not programmatically add a cron job. However, cron is a very important tool for system administrators and power users.

Timed Jobs Using launchd

Beginning in Mac OS X v10.4, the preferred way to add a timed job is to use a launchd timed job. A launchd timed job is similar to a cron job, with two key differences:

To create a launchd timed job, you should create a configuration property list file similar to those described in “Creating a Launchd Property List File” except that, instead of including an OnDemand key with a value of true, you specify a StartCalendarInterval key containing a dictionary of time values.

For example, the following property list runs the program happybirthday at midnight every time July 11 falls on a Sunday.

           <?xml version="1.0" encoding="UTF-8"?>
           <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
           http://www.apple.com/DTDs/PropertyList-1.0.dtd >
           <plist version="1.0">
           <dict>
                <key>Label</key>
                <string>com.example.happybirthday</string>
                <key>ProgramArguments</key>
                <array>
                     <string>happybirthday</string>
                </array>
                <key>OnDemand</key>
                <false/>
                <key>StartCalendarInterval</key>
                <dict>
                    <key>Hour</key>
                    <integer>00</integer>
                    <key>Minute</key>
                    <integer>00</integer>
                    <key>Month</key>
                    <integer>7</integer>
                    <key>Day</key>
                    <integer>11</integer>
                    <key>Weekday</key>
                    <integer>0</integer>
                </dict>
           </dict>
           </plist>

For more information on these values, see the manual page for launchd.plist.

Timed Jobs Using at

The at daemon is disabled by default in Mac OS X to reduce power consumption. If you need to enable it, you can learn how in the manual page for at.



< Previous PageNext Page > Hide TOC


© 2003, 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-19)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.