Blog

Creating Start Up Items in Mac OS X

There are three ways to get a program to run automatically when OS X starts up: Login items, Startup items, and launchd daemons. Startup items and launchd daemons are used to launch processes that run in the background like Apache or MySQL. Login items are meant for general applications (like Mail or Text Expander) that usually have some kind of user interface. I’ll show you how to create all three in this post.

Launching General Applications at Start up with Login Items

If you want to start an application (as opposed to a process or daemon) every time you login, simply add the application to the Login Items list in the Accounts preference pane in System Preferences.

Launching Background Processes with Startup Items and launchd Daemons

To launch a daemon or background process (like apache or MySQL) every time OS X starts, you must create either a launchd daemon (OS X 10.4+) or a Startup item (OS X 10.3 and earlier) depending on what version of OS X you are using. While you can still create Startup items in OS X 10.4 and later, Apple recommends using launchd configurations for the many advantages launchd provides. Check out Launching Custom Daemons Using launchd on the Apple Developer website for more information about launchd.

Create a launchd Daemon (OS X 10.4 and Later)

To create a launchd Daemon, you simply have to create a property list file in /Library/LaunchDaemons that points to the executable you want to run.

1. Create a properties list in /Library/LaunchDaemons.

sudo touch /Library/LaunchDaemons/com.apache.apache2svn.plist

2. Edit the plist and point it at your executable.

<?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.apache.apache2svn</string>
    <key>ProgramArguments</key>
	<array>
	    <string>/Users/mitch/dev/apache2/bin/apachectl</string>
	    <string>start</string>
	</array>
    <key>RunAtLoad</key>
	<true/>
    <key>KeepAlive</key>
	<true/>
</dict>
</plist>

Label is a unique name to identify your daemon.
Program Arguments is the path to the executable you want to run.
RunAtLoad tells launchd to run your daemon when OS X starts up.
KeepAlive tells launchd that the process should remain running indefinitely (launchd can run process on demand and terminate them after running, KeepAlive tells launchd that this is not an on demand process).

3. Restart your computer and your process should be running.

Create a Startup Item (OS X 10.3 and Earlier)

To create a Startup item, you need to create a new directory under /Library/StartupItems that contains a properties list file and a shell script. The shell script should be the same name as the directory. The property list must be named StartupParameter.plist.

1. Create a directory under /Library/StartupItems.

sudo mkdir /Library/StartupItems/apache2svn

2. Create an executable with the same name as the directory.

sudo touch /Library/StartupItems/apache2svn/apache2svn
sudo chmod a+x /Library/StartupItems/apache2svn/apache2svn

3. The executable should look something like this. Each function either starts, stops, or restarts your process. You can leave the function body blank if it does not apply to your process. Don’t forget to include the . /etc/rc.common and RunService "$1" lines.

#!/bin/sh
. /etc/rc.common
 
StartService ()
{
    /Users/mitch/dev/apache2/bin/apachectl start
}
 
StopService ()
{
    /Users/mitch/dev/apache2/bin/apachectl stop
}
 
RestartService ()
{
    /Users/mitch/dev/apache2/bin/apachectl graceful
}
 
RunService "$1"

4. Create a properties list called StartupParameter.plist.

sudo touch /Library/StartupItems/apache2svn/StartupParameter.plist

5. The property list should look something like this.

<?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>Description</key>
	<string>Apache2 (SVN on Port 8080)</string>
    <key>OrderPreference</key>
        <string>None</string>
    <key>Provides</key>
	<array>
	    <string>apache2svn</string>
	</array>
</dict>
</plist>

Description is a human readable description of your process.
Providesis a unique name that identifies your process.

6. You can test your startup item using the SystemStarter command.

sudo /sbin/SystemStarter start "apache2svn"

7. Otherwise, restart your computer and you’re process should be running after boot up.

Leave a comment below if you’re having problems getting you’re process to run.

One Response

  1. 1

    Very useful. Thanks.

    One small error that took me some time to spot: the parameter file should be called StartupParameters.plist (plural). This may have been correct at the time the article was written but on MacOSX 10.8 it’s definitely important to add that ‘s’.

Leave a Reply to Carl Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">