How to run a script at start-up

Startup items are directory bundles which contain, at a minimum, an executable file and a property list text file. For a startup item named "Foo", the bundle will be a directory named "Foo" containing (at a minimum) an executable "Foo" and a plist file "StartupParameters.plist".

  1. Mkdir a new directory for your script in the startup folder /Library/StartupItems E.g.
    mkdir /Library/StartupItems/Greeting
  2. Put your script in the directory you just created, in a file of the same name. e.g.
    #!/bin/sh
    
    ##    
    # Do my Greeting
    ##    
    
    . /etc/rc.common
         
    if [ "${FIREWALL:=-NO-}" = "-YES-" ]; then 
        ConsoleMessage "Adding Firewall Rules"
         
       ipdeny=`cat /Documents/Firewall/Firewall.deny | sort | uniq` 
       ipallow=`cat /Documents/Firewall/Firewall.allow | sort | uniq` 
         
       # allow first
       counter=2000
       for i in ${ipallow}; do
        ipfw add $counter allow all from $i to any
        counter=`expr $counter + 1`
       done    
         
       # deny second
       counter=`expr $counter + 1000` 
       for i in ${ipdeny}; do
        ipfw add $counter deny all from $i to any
        counter=`expr $counter + 1`
       done    
         
    fi
    
    "chmod +x Greeting" the shell script file

  3. Also in the dir you just created, add these (or similar) lines to file StartupParameters.plist
    {
      Description     = "HAL9000 greeting";
      Provides        = ("Greeting");
      OrderPreference = "Last";
      Messages =
      {
        start = "speaking greeting";
        stop  = "greeting going away";
      };
    }
    
    The above example is written in the old NeXT-style property list format for compactness. But the new XML property lists are also handled. You may prefer using PropertyListEditor.app to editing the property list files manually.

    The plist file contains parameters which tell SystemStarter some information about the executable, such as what services is provides, which services are prerequisites to its use, and so on. The plist contains the following attributes: Description, Provides, OrderPreference, etc.

  4. chmod -R 755 /Library/StartupItems/Greeting
    chown -R root:admin /Library/StartupItems/Greeting
    chgrp -R root:admin /Library/StartupItems/Greeting
    For security reasons (because they are executed by root), these startup bundles don't execute unless root owns them.

  5. As root, edit /etc/hostconfig which defines several Yes or No variables. Add the "GREETING=-YES-" line.

Startup items may be placed in the "Library" subdirectory of the primary file domains ("System", "Local", and "Network"). The search order is defined by routines defined in NSSystemDirectories.h: Local, then Network, then System. However, because the Network mounts have not been established at the beginning of system startup, bundles in /Network is currently not searched.


See http://www.publicsource.apple.com/tools/cvs/ for more info on getting sources from Darwin CVS.