Load or Reload a LaunchAgent from installer script

Apple has some restrictions in place to prevent access to LaunchAgents running in a user session context.

But you may want to load or refresh a LaunchAgent as part of your install without requiring the user to log out and back in.

I prefer not to require logouts and reboots in my installation packages.  Where possible, I use munki’s unattended option so software installs silently and the user is not prompted.

After some experimentation, I came up with this hacky method of getting a LaunchAgent to load from a package being installed as root.  If you have a cleaner way to accomplish this, please let me know.  Update: Please see Per Olofsson’s comment for a much better method until I update this gist.


#!/bin/bash
# if someone is logged in
if who | grep -q console; then
# get the logged in user's uid
LOGGED_IN_UID=`ls -ln /dev/console | awk '{ print $3 }'`
# use the uid and pgrep to find the Finder process id
FINDER_PID=`pgrep -U ${LOGGED_IN_UID} Finder`
# use launchctl bsexec to run applescript code in the same Mach bootstrap namespace hierachy as the Finder
launchctl bsexec "${FINDER_PID}" osascript -e '
tell app "Finder"
do shell script "
launchctl unload -S Aqua /Library/LaunchAgents/com.company.agent.plist
launchctl load -S Aqua /Library/LaunchAgents/com.company.agent.plist
"
end tell
'
fi

view raw

gistfile1.sh

hosted with ❤ by GitHub

 

dockutil 1.1 released

Version 1.1 of dockutil is out:

  • fixes many issues with paths (should now work with Default User Template and other paths with spaces)
  • adds option to not restart the dock (–no-restart)
  • fixes issue where item would be added multiple times (use –replacing to update an existing item)
  • resolves deprecation warnings
  • adds option to remove all items (–remove all)
  • fixes issue with removals when a url exists in a dock
  • adds option –version to output version

Fix Apache mod_jk or mod_proxy serving stale content

If your web app starts serving stale cached content when run behind mod_jk or mod_proxy with apache, it may be due to apache inserting a default expiration header.

You can confirm this by comparing the headers returned from apache and directly from your web app.  curl -i will show response headers:

curl -i http://example.com | head -20

To disable apache’s content expirations, add the following to your virtual host:

ExpiresActive Off

Here is the official Apache Documentation.

MIME type issue with Apache mod_jk and mod_proxy serving plain text

Some apps do not properly set mime types of content they serve, but still may work properly when served standalone because client applications like browsers are able to interpret the type of the content.  But when served behind Apache, these apps will not behave correctly because Apache will provide a default type of text/plain.

The solution is to add a DefaultType None line to your apache virtual host for these web apps:

DefaultType None

Here are the docs

Parsing Mac OS X System Profiler

It is pretty cool how Apple System Profiler has a command line equivalent (system_profiler). And it is pretty cool how system_profiler has a -xml option to allow for easier parsing. You might use this info for extracting asset information into a database or for puppet facter facts.

However if you’ve ever looked at that xml, you know that it is a tree full of unpredictable semi-structured data that was designed specifically for the GUI app. So even though you can parse it with your favorite plist parser, there is still a lot more work to do to get to the data you care about.

The tree structure is nice for a browsing through on a single machine, but not so good for reporting across many machines.

Apple stores most of the same data as key value pairs in its database for ARD reporting, but they do a lot of massaging of the data to get it that way.

It is possible to get at this data in an ARD database if you have an ARD collection server, but an ARD collection server isn’t for everyone and doesn’t serve every use case.

You can still get at the nicely formatted ARD information. ARD client includes a tool that outputs most, if not all of the asset information you care about in a much nicer structured format for reporting.

The tool is called sysinfocachegen and you use it like this:

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/sysinfocachegen -p /tmp/com.yourorganization.systeminfo.plist

Just use your favorite language’s plist parser to read the plist.

Disable protect_from_forgery when load testing rails

Rails turns on protection from CSRF Cross-Site Request Forgery by default. It can make load testing more challenging since you need to get an authenticity_token for posting form data.

More information here: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html