MAC OSX BOOT sequence ( launchd )

Os x boot sequence  : what is launched at startup of your Mac can be easily see :
"System Preferences" -> "Users & Groups" -> "Login Items"

OSX Login Items (preference panel)
OSX Login Items (preference panel)

However not everything is there.

#1 MAC Boot sequence

Who knows boot sequence of Linux will find similar the MAC OSX startup sequence...
The sequence :
OSX boot sequence
OSX boot sequence

In the "power on" step (as you probably figured out) EFI selects the right device to use : you can overwrite this pressing "options" key during startup. Btw after that BootROM, EFI, Boot Loader and  Kernel aall of them have done their job the control passes to launchd process.
Launchd is in fact the first process that Kernel starts (PID is 1) and it starts the "user space" boot sequence

busycrack$ ps aux | grep launchd
root               1   0.0  0.1...0:07.95 /sbin/launchd
So Launchd has in charge to start System and Users daemon/agent basing its task on plist files here :
~/Library/LaunchAgents Per-user agents provided by the user
/Library/LaunchAgents  Per-user agents provided by the administrator
/Library/LaunchDaemons System-wide daemons provided by the administrator  
/System/Library/LaunchAgents    Per-user agents provided by Mac OS X
/System/Library/LaunchDaemons  System wide daemons provided by MAC OS X

For example in my home directory I have:

busycrack$ cd ~/Library/LaunchAgents && ls -ltra
total 24
-rw-r--r--@  1 busycrack  staff   806 Jul 11 20:59 com.google.keystone.agent.plist
-rw-r--r--   1 busycrack  staff   697 Jul 20 22:25 com.*****.XXXXX.YYYYYYY-1.0.plist
drwx------@ 55 busycrack  staff  1870 Jul 28 18:06 ..
-rw-r--r--   1 busycrack  staff   606 Jul 28 18:40 de.metaquark.appfresh.plist
Launchd is controlled by command launchctl ... below some notes

#a) list all with command "lauchctl list"

busycrack$ launchctl list 
...
-       0       de.metaquark.appfresh
581     -       [0x0-0x4b04b].de.metaquark.appfresh
-       0       com.apple.metadata.mdwrite
...

Where :

PID Last Exit Status Job Label
581  - [0x0-0x4b04b].de.metaquark.appfres
 0 de.metaquark.appfresh

#b) get details:
"lauchctl list de.metaquark.appfresh"

busycrack$ launchctl list de.metaquark.appfresh
{
        "Label" = "de.metaquark.appfresh";
        "LimitLoadToSessionType" = "Aqua";
        "OnDemand" = true;
        "LastExitStatus" = 0;
        "TimeOut" = 30;
        "ProgramArguments" = (
                "/Applications/AppFresh.app/Contents/Resources/AppFreshDaemon";
                "/Applications/AppFresh.app";
        );
};

#c) list all daemons not by apple :
"launchctl list | grep -v com.apple"

In order to discover not apple daemon generally  :
busycrack$ launchctl list | grep -v com.apple
PID     Status  Label
216     -       com.MadCatz.SmartTechnology
-       97      com.adobe.CS5ServiceManager
-       0       com.google.keystone.user.agent
...
Note : try also "sudo launchctl bslist | grep -v apple"

#d) remove : "launchctl remove de.metaquark.appfresh"
#e) disable  : "launchctl unload -w ~/Library/LaunchAgents/..."

How disable Deamon/Agent at startup?

You can remove .plist file from directory or you can use launchctl:
launchctl unload -w ~/Library/LaunchAgents/com.google.keystone.agent.plist
In theory this should unload and disable the service adding Disabled Key :
less ~/Library/LaunchAgents/com.google.keystone.agent.plist

 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
  <dict>
  <key>Disabled</key> 
  <true/>
  <key>Label</key>
  ...
  ...
Really what happen is that file com.google.keystone.agent.plist remains unchanged and what change is overrides.plist.
File overrides.plist is present in : 
 /private/var/db/launchd.db/com.apple.launchd/overrides.plist
    or in directory :
  /private/var/db/launchd.db/com.apple.launchd.peruser.501/overrides.plist
Position depends by domain of daemon/agent  if it is System or Per-user.
Note peruser.501 : 501 (com.apple.launchd.peruser.501) is id of user :
busycrack$ id
uid=501(busycrack) gid=20(staff) groups=20(staff)...
Btw file overrides.plist  reports values which are overriding the values in main .plist :
launchctl load -w ~/Library/LaunchAgents/com.google.keystone.agent.plist
less /private/var/db/launchd.db/com.apple.launchd.peruser.501/overrides.plist
...
<key>com.google.keystone.user.agent</key>
 <dict>
 <key>Disabled</key>
 <false/>
</dict>
---------------------------
launchctl unload -w ~/Library/LaunchAgents/com.google.keystone.agent.plist
less /private/var/db/launchd.db/com.apple.launchd.peruser.501/overrides.plist
...
<key>com.google.keystone.user.agent</key>
 <dict>
 <key>Disabled</key>
 <true/>
</dict>

However for this and much more things :

  • man launchd
  • man launchctl

#2 Bonus

2.1) try command :  sudo launchctl bstree -j

Customizing Login and Logout :
2.2) you can check present of login script with command : LoginHook

defaults read com.apple.loginwindow LoginHook

Script (if present) which is attached to user login... more details : http://support.apple.com/kb/HT2420

2.3) logout script : LogoutHook

defaults read com.apple.loginwindow LogoutHook

Script (if present) which is attached to user logout

#3 App GUI for lauchdctl

lingon : http://sourceforge.net/projects/lingon/files/latest/download 

Lingon
Powered by WP Review