4.2. Adding Startup Items

 < Day Day Up > 

To automatically start applications, you have two choices: start them when a user logs in, or start them when the system boots up. On most Unix systems, startup applications either reside in the /etc/rc.local script or the /etc/init.d directory. Under Mac OS 9, you could add a startup item by putting its alias in System Folder/Startup Items. Mac OS X has a different approach, described in the following sections.

4.2.1. Login Preferences

To start an application each time you log in, use the Accounts panel of System Preferences and select the Login Items tab. This is good for user applications, such as Stickies or an instant messenger program. For system daemons, you should set up a directory in /Library/StartupItems, as described in the next section.

4.2.2. Startup Items

If you compile and install a daemon, you'll probably want it to start at boot time. For example, MySQL will build out of the box on Mac OS X (you can download it from http://www.mysql.com).

In some cases, you can start a daemon by creating a launch daemon property list in /Library/LaunchDaemons. However, there are many restrictions on launch daemons for example, they are not allowed to change the user or group id. Also, launch daemons do not have a facility for shutting down. For complete details on these restrictions, see the launchd.plist manpage. If you are setting up a daemon that either cannot abide by the launchd restrictions, or one that needs to be shutdown gracefully, you should create a Startup Item as described in this section.


A startup item is controlled by three things: a folder (such as /Library/StartupItems/MyItem), a shell script with the same name as the directory (such as MyItem), and a property list named StartupParameters.plist . The shell script and the property list must appear at the top level of the startup item's folder. You can also create a Resources directory to hold localized resources, but this is not mandatory.

To set up the MySQL startup item, create the directory /Library/StartupItems/MySQL as root. Then, create two files in that directory, the startup script MySQL and the property list StartupParameters.plist. The MySQL file must be an executable since it is a shell script. After you set up these two files as directed in the following sections, MySQL is launched at each boot.

4.2.2.1. The startup script

The startup script should be a shell script with StartService(), StopService( ), and RestartService( ) functions. The contents of /Library/StartupItems/MySQL/MySQL are shown in Example 4-3. The function call at the bottom of the script invokes the RunService() function from rc.common, which in turn invokes StartService( ), StopService( ), or RestartService( ), depending on whether the script was invoked with an argument of start, stop, or restart.

Although previous versions of Mac OS X did not invoke the StopService( ) code when the system was shut down, Mac OS X Tiger does. Database developers everywhere can breathe a sigh of relief.


Example 4-3. A MySQL startup script
     #!/bin/sh     # Source common setup, including hostconfig.     #     . /etc/rc.common     StartService( )     {         # Don't start unless MySQL is enabled in /etc/hostconfig         if [ "${MYSQL:=-NO-}" = "-YES-" ]; then             ConsoleMessage "Starting MySQL"             /usr/local/mysql/bin/mysqld_safe --user=mysql &         fi     }     StopService( )     {         ConsoleMessage "Stopping MySQL"         # If you've set a root password within mysql, you may         # need to add --password=password on the next line.         /usr/local/mysql/bin/mysqladmin shutdown     }     RestartService( )     {         # Don't restart unless MySQL is enabled in /etc/hostconfig         if [ "${MYSQL:=-NO-}" = "-YES-" ]; then             ConsoleMessage "Restarting MySQL"             StopService             StartService         else             StopService         fi     }     RunService "$1" 

Because it consults the settings of the $MYSQL environment variable, the startup script won't do anything unless you've enabled MySQL in the /etc/hostconfig file. To do this, add the following line to /etc/hostconfig:

     MYSQL=-YES- 

Mac OS X does not recognize any special connections between hostconfig entries and startup scripts. Instead, the startup script sources the /etc/rc.common file, which in turn sources hostconfig. The directives in hostconfig are merely environment variables, and the startup script checks the value of the variables that control its behavior (in this case, $MYSQL).

4.2.2.2. The property list

The property list (StartupParameters.plist) can be in XML or NeXT format , and the list contains attributes that describe the item and determine its place in the startup sequence. The NeXT format uses NeXTSTEP-style property lists, as shown in Example 4-4.

Example 4-4. The MySQL startup parameters as a NeXT property list
     {       Description     = "MySQL";       Provides        = ("MySQL");       Requires        = ("Network");       OrderPreference = "Late";     } 

The XML format adheres to the PropertyList.dtd Document Type Definition (DTD) . You can use your favorite text editor or the Property List Editor (/Developer/Applications/Utilities) to create your own property list. Example 4-5 shows the property list in XML.

Example 4-5. The MySQL startup parameters as an XML property list
     <?xml version="1.0" encoding="UTF-8"?>     <!DOCTYPE plist       SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">     <plist version="0.9">     <dict>         <key>Description</key>         <string>MySQL</string>         <key>Provides</key>         <array>             <string>MySQL</string>         </array>         <key>Requires</key>         <array>             <string>Network</string>         </array>         <key>OrderPreference</key>         <string>Late</string>     </dict>     </plist> 

The following list describes the various keys you can use in a startup parameters property list:


Description

This is a phrase that describes the item.


Provides

This is an array of services that the item provides (for example, Apache provides Web Server). These services should be globally unique. In the event that SystemStarter finds two items that provide the same service, it starts the first one it finds.


Requires

This is an array of services that the item depends on. It should correspond to another item's Provides attribute. If a required service cannot be started, the system won't start the item.


Uses

This is similar to Requires, but it is a weaker association. If SystemStarter can find a matching service, it will start it. If it can't, the dependent item still starts.


OrderPreference

The Requires and Uses attributes imply a particular order, in that dependent items will be started after the services they depend on. You can specify First, Early, None (the default), Late, or Last here. SystemStarter does its best to satisfy this preference, but dependency orders prevail.

You can now manually start, restart, and stop MySQL by invoking SystemStarter from the command line:

     $ sudo SystemStarter start MySQL     $ sudo SystemStarter restart MySQL     $ sudo SystemStarter stop MySQL 

     < Day Day Up > 


    Mac OS X Tiger for Unix Geeks
    Mac OS X Tiger for Unix Geeks
    ISBN: 0596009127
    EAN: 2147483647
    Year: 2006
    Pages: 176

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net