EGOPOLY

Topics include: programming, Apple, Unix, gadgets, large-scale web sites and other nerdy stuff.

Add your own boot command script to Mac OS X

2008-03-26 15:26:06

If you have some things you want to start when your mac boots, here is how to do it. I use mine to start up an SSH keychain, start a mysql server and a memcached.

First, you'll need to be root:

sudo tcsh

Now, make a directory for your stuff. Suppose your name is "Buffy."

mkdir /Library/StartupItems/buffy chown root /Library/StartupItems/buffy chmod 755 /Library/StartupItems/buffy

You need to put two files in this directory. The first one is called StartupParameters.plist and should look like this:

{ Description = "buffy"; Provides = ("buffy"); Requires = ("Network"); OrderPreference = "Late"; Messages = { start = "Starting buffy"; stop = "Stopping buffy"; }; } If you were some kind of Darwin super guru, you could set all kinds of other options in here, but let's pretend for now that you just want to save yourself remembering to type extra stuff after you reboot your mac. Crap that sentence was ugly. Sorry.

OK, now you need to write the actual script to start things. name that file buffy

Here is an example:

#!/bin/sh

##
# buffy local stuff
##

. /etc/rc.common

StartService ()
{
    ConsoleMessage "buffy startup"
    # start postfix mail server
    postfix start
    # this is some weird stuff that only i'm interested in
    # but I'm sure  you have similar weird stuff
    /r9/mysql1/bin/r9mysqld start
    export EVENT_NOKQUEUE=1
    # note the 'su' things so these process run as the 'buffy' user
    su buffy -c '/usr/local/bin/memcached -d -m 128 -p 11211'
    su buffy -c 'sh /Users/billo/bin/keychain'
}

StopService ()
{
    ConsoleMessage "buffy shutdown"
    /r9/mysql1/bin/r9mysqld stop
}

RestartService ()
{
    StopService
    StartService
}

RunService "$1"

Make sure the files you create are owned by 'root' otherwise OS X will ignore them.