[MacTUG] playing with launch agents

Donald Duff-McCracken dsmccrac at uwaterloo.ca
Tue Oct 1 14:49:13 EDT 2013


I know some folks out there are familiar with launchagents and daemons (such as James who fielded a few questions when I started playing with them – thanks BTW), but in case anyone cares, here are my rather lengthy notes (to myself and coworkers) on what I learned successfully migrating my loginhook (which is deprecated) to a set of launchagents and launchdaemons:
_____________________________

Some good bits of info:  http://launchd.info/,  http://nathangrigg.net/2012/07/schedule-jobs-using-launchd/ and http://wiki.afp548.com/index.php/Launch_Daemons/Agents_in_OS_X

LaunchAgents vs LaunchDaemons: LaunchAgents often run at login and they are run by the user that logs in. LaunchDaemons are often run at startup and run as root. I say "often" because they always need to have specified when they run, even with their 'normal' usage (of Login for a LaunchAgent and Startup for a LaunchDaemon). Don't assume that they will run anytime by default without anything specified.  For example for launchagents:

  *   If you want a LaunchAgent to run at login (what you would assume is a default thing) you need to specify it with a

<key>RunAtLoad</key>
<true/>

  *   If you want a launchagent to keep running forever (restarting if needed) after login you must specify
<key>KeepAlive</key>
<true/>
  *   If you want a launchagent to keep running (restarting if needed) after login until it succeeds you must specify
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>

When migrating from a loginhook (which run as root), you may need some actions done be a launchdaemon (which runs as root), similarly some tasks may be more appropriate for a launchagent (where you can or need to run it as the user, or you need to access the GUI which a launchdaemon cannot do as well). I broke my loginhook down into 2 agents and 2 daemons.

One Issue I had a hard time figuring out was how to get something to run at login but run as root (as I needed some cleanup done that only root could do). It seemed that I could not do this elegantly as LaunchAgents can run at login but LaunchDaemons cannot seem to do so. For sure you cannot use a LaunchAgent as they run as the user.  An inelegant way would be to have the LaunchDaemon run every X minutes (which google's launch agent does, of course), but that is a waste of resources. Fortunately LaunchAgents and Daemons can be told to run when a new volume is connected
<key>StartOnMount</key>
<true/>
And fortunately when the user logs in their N-Drive auto mounts so this can initiate the launchdaemon to run!

There are a couple of other neat tricks you can do with these agents and daemons, such as telling it to WatchPath and run when you see a change in this path. I think that is pretty slick.

I never have not used a logouthook for a long time — in fact the only reason I used one (vs having it happen at login) was to defer some tasks to logout to speed up the login process (but with new processors this is not an issue). However, if I did want a LaunchAgent to deal with actions at logout (and it was a task that I could not do at login) I am not quite sure how I would proceed. But this is theoretical as I do not perform any logout actions.


------------------------------------
Donald Duff-McCracken
Technical Services Manager
Mapping, Analysis & Design
Faculty of Environment
University of Waterloo
(519) 888-4567 x32151
https://uwaterloo.ca/environment-computing/about/people/donald-duff-mccracken

------------
To request help from MAD please use Request Tracker. For info see:
https://rt.uwaterloo.ca/~wwwrt/cgi-bin/rtuser.pl

------------
This email communication is intended as a private communication for the sole use of the primary addressee and those individuals listed for copies in the original message. The information contained in this email is private and confidential and If you are not an intended recipient you are hereby notified that copying, forwarding or other dissemination or distribution of this communication by any means is prohibited.  If you are not specifically authorized to receive this email and if you believe that you received it in error please notify the original sender immediately.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.uwaterloo.ca/pipermail/mactug/attachments/20131001/b19d27e4/attachment.html>


More information about the MacTUG mailing list