DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OS X | UNIX

back

Unix

Mac OS X How To

by Dr John Timmer - Contributing Editor

Using Startup Items in Mac OS X

Introduction

In MacOS-9, startup items were pretty simple affairs. All applications were treated roughly equally, and placing any of them in the Startup Items folder was enough to get them launched as soon as the Finder started up. In MacOS-X, things are a bit more complicated - startup items may run in the Unix layer or as a standard Macintosh application. In addition, they may run as any one of a number of users, from root down to a user without administration abilities. In the following tutorial, I'll explain the differences between the different types of startup items and how to create and use them.

Starting up GUI based Applictions

GUI based and other standard Macintosh applications are the simplest to set as startup items. GUI based applications are extremely difficult to start unless you're logged in, but you can start just about any Mac application at login using the System Preferences application. Prior to 10.2, you can select the "Login" panel, and choose the "Login Items" tab. In Jaguar, Login Items have their own Preference Pane. In either case, as shown below, there's an "Add" button that will allow you to browse to the application of your choice. Once that's done, the application should launch every time you log in or, if you've set your computer up to automatically log you in, after every restart.

Starting up Unix Applications

Unix applications are a different story entirely. Since they don't interact with the GUI layers of the OS, they can be run as any user on the system. Typically, however, there are two users that you'd want to run a Unix task as: yourself and root. Run something as yourself if you'd like to modify files or behaviors that are specific to your user account, or if there's a service you want to ensure is available during your login session. Run something as root if you need to modify a system-wide behavior or provide a service to all users of the machine (including those over the network), regardless of whether anyone is logged in.

Performing Unix commands at login: Perhaps the simplest way of executing a series of Unix commands is to create shell script. At their simplest, these scripts are text files that typically start with the line:

#! /bin/sh

which identifies them as a shell script and sets which program will interpret the script (in this case, the standard "sh"). The remainder of the script will generally consist primarily of Unix commands. Thus, if you have some commands you need executed, you can simply type them in below this line, and save the text file. After you save the file, change it's name to include the extension ".command" (ie - "startup.command") and use chmod to make it executable ("chmod +x startup.command"). This will indicate to the Finder that the file should be opened and executed as a Terminal document. You can then choose it as a login item as detailed above to launch Terminal and execute these commands once you login.

An alternative that avoids the time spent launching Terminal and resulting window clutter is to write an AppleScript that will execute the commands you need and save that as an application. You can see some of my previous tutorials for information on how to send Unix commands via AppleScript using the "do shell script" command. After confirming that your AppleScript has the desired effect, save the script as an application by selecting "Save As Run-Only" from the "File" menu. Use the "Format:" pop-up menu to select "Application", and click the "Never Show Startup Screen" option that will appear at the bottom. You can then put the application anywhere you'd like and then select it from the "Login" panel of System Preferences as described above.

Performing Unix commands at system startup:

All Unix commands run while OS-X is booting up are run as root and have the potential to significantly alter the behavior of your system. Because this is potentially dangerous, creating one of these startup items is fairly difficult. They have a very structured format, and will not load unless they reside in one of two places: /System/Library/StartupItems (reserved for those provided by Apple) or /Library/StartupItems (where you should put yours).

In this example, we'll go through the process of creating a startup item that will launch the MySQL database server when the computer is booted. The easiest way to create a startup item is to use an existing one as a template. Fortunately, there are a number of them in /System/Library/StartupItems - we'll choose Sendmail, since it's somewhat similar to MySQL in that it provides an optional network-based service. Since we don't want to accidentally damage an existing Startup Item, use the Finder to navigate to /System/Library/StartupItems and option-drag a copy of the Sendmail folder to your desktop.

The first step will be to rename the folder to something more appropriate, such as "MySQL". Once that's done, open the folder. You should see three files: A Resources folder, a file called "StartupParameters.plist", and a file that matches the initial name of the enclosing folder ("Sendmail", assuming you're following these instructions). First, rename that file to exactly match whatever you've called the folder (ie - "MySQL", remembering that this is Unix, and the capitalization is likely to count). Your folder should now look like this:

It's time to start editing the contents of these files. We'll start with the "Resources" folder, which should contain ".lproj" folders for one or a number of languages. Throw the folders for any language you can't speak out. Open the English folder, and you should find a file called "Localizable.strings". Open this in a text editor, and you'll find it's an XML file that contains a startup message ("Starting Sendmail") both as a key and as a string. When the OS is executing this startup item, it checks for occurrances of the key string and, depending on the language the system is set to, replaces it with the corresponding string if available. Change the key and the string to "Starting MySQL" and save the file. Now, navigate to any other languages you didn't throw out and open the equivalent files; change the key string to "Starting MySQL", and the other string to something appropriate for the language.

Our next stop will be the "StartupParameters.plist" file. If you have the developer tools installed, this will open in PropertyListEditor, but any text editor that saves raw text files will do. It may come in one of two forms: XML or a more readable text form. Both will contain the same information, as depicted below:

The first thing to do is to change the "Messages" value for "start" - instead of "Starting Sendmail", change this to whatever you changed the key phrase to above, presumably "Starting MySQL". You can also change the "stop" message, but this feature is not currently implemented, so you won't see this message until a future version of OS-X.

The loading of Startup Items is intelligent, in that you can set them to only load after other system services that they need in order to function properly. Each Startup Item has a "provides" field to tell the system what services it provides, and a "requires" field to tell it what other items must start before it. For MySQL, we'll change the "provides" string to "MySQL". That way, if you ever make another startup item that needs MySQL running to work, you can have its "Requires" field set to "MySQL". Our Startup Item's "requires" string starts out set to "Resolver", which is appropriate for MySQL, so we'll leave it unchanged. The "Preference" key is actually a typo - change it to "OrderPreference". This value gives the OS a rough idea of when to start looking into loading something; since a database is generally not fundamental to an OS's function and needs a lot of things working properly first, we can set that to "Late" or "Last". You can also leave the "Uses" values the same ("Network Time" and "NFS"), as they're things that could conceivably affect the behavior of MySQL in some circumstances. Finally, you can change "Description" string to something appropriate, such as "MySQL database server" and then save the file.

Finally, we have to edit the "MySQL" file, which is the actual executable shell script that launches MySQL. We're going to keep this simple, and have it execute the commands with which I normally start the database from within Terminal:

cd /usr/local/mysql
sudo ./bin/safe_mysqld --user=mysql &

The second command, executed as root, executes a script which launches the database in a secure manner. As part of its security measures, it requires that the script be launched from the mysql directory - the first line simply goes to that directory.

In the "MySQL" script, you shouldn't edit the first few lines - these identify this as a shell script and include some useful startup functions - but change the "# Start mail server" comment to something more appropriate, such as "# Start MySQL database server". Delete the line which starts with "if", and change the ConsoleMessage to "Starting MySQL". Delete the whole rest of the script - we'll simply start MySQL with the commands we'd use to do so in the terminal:

cd /usr/local/mysql; ./bin/safe_mysqld --user=mysql &

Note that we've skipped using "sudo" - startup items are automatically executed as root. Distilled to its simplest, the script should look like:

#!/bin/sh
. /etc/rc.common
ConsoleMessage "Starting MySQL"
cd /usr/local/mysql; ./bin/safe_mysqld --user=mysql &

Save the changes, and your startup item is almost ready. Next, you have to place it in the startup items folder. Prior to 10.2, this was as simple as dragging an item to the folder /Library/StartupItems. As a security measure, 10.2 changed the ownership of /Library/StartupItems to "root", making it harder to modify. In order to place your startup item there, you have to execute a terminal command as root, using "sudo". Start by typing:

sudo cp

Next, drag the startup item folder to the terminal window - the path to the folder should appear on the line you were typing. After that, type:

/Library/StartupItems/MySQL

and hit return. This will place your startup item into the right folder. A further security measure is that no startup item will launch unless it's owned by root. To make sure your item launches, type the following commands:

sudo chown -r root /Library/StartupItems/MySQL/*
sudo chgrp -r admin /Library/StartupItems/MySQL/*

Your permissions should now look like this:

-rwxr-xr-x 1 root admin 1407 Dec 2 2001 MySQL
drwxr-xr-x 9 root admin  262 Dec 2 2001 Resources
-rw-r--r-- 1 root admin  216 Dec 2 2001 StartupParameters.plist

By choosing carefully among these options, you should be able to make sure OS-X has all the capabilities and services you need waiting for you whenever you access it.

end

Copyright © 2000-2011 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iPhone, Apple TV, iPad, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.