LaunchDaemons vs LaunchAgents

launchdaemons vs launchagents

techjournal.318.com has an nice short summary of the difference between LaunchDaemons and LaunchAgents on Mac OS X.  I thought I would recap here in my own words for our followers as well as flush out the concepts a little more.

LaunchDaemons – Run at Boot

LaunchDaemons are processes managed by the launchd service in OS X that are loaded when the system boots.  The most significant shortcoming about LaunchDaemons is that they can’t display information to the GUI (Graphical User Interface).  This is a restriction because there may, or may not be, a graphical interface to display to; because there may, or may not be, a user logged in while these processes are running.

To make a LaunchDaemon you create a specially formatted plist and do the following:

  1. It is put it in the folder /Library/LaunchDaemons/
  2. Make sure it is owned by root and in the group wheel
  3. Has the permissions “644”: Owner – read/write, group – read, everyone – read

LaunchDaemons are plists stored in /Library/LaunchDaemons/ (For installed software and our own custom plists) or in /System/Library/LaunchDaemons/ (For OS X native processes only).  You can add your own plists to the /System/Library/LaunchDaemons/ folder, but it isn’t advised.  That folder has been known to be over written during system updates so you could lose you work if you put it there.  That is why /Library/LaunchDaemons/ exists.  To be our special own playground…

LaunchAgents – Run at Login

LaunchAgents are processes managed by the launchd service in OS X as well, but they are loaded when a user logs in rather then at system boot.  Processes launched as LaunchAgents are able to access the system GUI and display information to it.  The shortcoming here, of course, is that these process will not run until a user logs in.

To make a LaunchAgent you create a specially formatted plist and do the following:

  1. It is put it in one of these folders:
    1. /Library/LaunchAgents/  – (For all users)
    2. ~/Library/LaunchAgents/  – (For a specific user)
    3. /System/Library/LaunchAgents/ – (For OS X use only)
  2. Make sure it is owned by root and in the group wheel
    1. NOTE: The one exception to this is if it is in ~/Library/LaunchAgents/. If there, it should be owned by the user that owns the specific home directory.
  3. Has the permissions “644”: Owner – read/write, group – read, everyone – read

As with LaunchDaemons, the /System/Library/LaunchAgents/ folder can be used, but really shouldn’t be.  Use the other folder options instead.

What They Both Do

Both LaunchAgents and LaunchDaemons are plists that are loaded into memory by launchd.  These plists have a strict structure, but at the same time have a ton of available keys.  Many of these keys are triggers that can make your script/application/etc. run when a file or a directory is edited (“WatchPaths”), or run every so often (“StartInterval”), or at a certain date and time (“StartCalendarInterval”) , and so on.  There is a nice list over on the Apple developer site if you want to see what other keys are available.  The keys are the same for both LaunchDaemons and LaunchAgents.  Really the difference is when they are run and not in their structure.

Where Does Launchctl Come Into the Picture?

launchctl is the command line tool that you can use to manually load or unload a launchd plist into memory.  So, you can use launchctl to activate a new launchd plist you just created without having to log out and back in again or restart your computer.  You can issue something like the following to load a launchd plist into memory on your system without having to log out or reboot.

As a side note, launchctl can load both LaunchAgent plists and LaunchDaemon plists.

Conclusion

Let me sum up:

  1. LaunchDaemons are loaded at system boot
  2. LaunchAgents are loaded when a user logs in
  3. launchctl is a command line tool to manually load and unload LaunchDaemon or LaunchAgent plists.



One Last Thing

Need to hash out more with a human being? We are available for consulting!
Feel free to contact us about consulting or just to say hi.