To make an automatically executing per-user job (LaunchAgent) for every user:
- Read man launchd.plist. Place a job (i.e. plist for launchd) in ~/Library/LaunchAgents.
- There is a naming convention for plists. Save your plist as org.yourdomain.plist (name yourdomain descriptive to the job).
- Test it with launchctl load ~/Library/LaunchAgents. Remember to unload for edits. Sometimes a rogue LaunchAgent resists unload, I had to provoke error until launchd terminated it. Look at Console messages.
- For example, launchd does not like quoted path strings in plist file. Nor binary plists made by defaults write (at least on Mac OS X 10.5).
- Unload the working plist, then copy it to /Library/LaunchAgents (authenticate when asked).
- Change the owner of the plist file to "system" ie. chown root:admin /Library/LaunchAgents/org.yourdomain.plist (At least Mac OS X 10.4 disliked a /Library job owned by an admin user).
- Set the permissions of the plist file to read only for others than owner: sudo chmod 644 /Library/LaunchAgents/org.yourdomain.plist
Example:
Automatically mount a disk image for every user. Tuomas Rosberg 2010.
Runs hdiutil to mount a disk image at login and when there is a change in /Volumes/.
Effectively keeps the disk image mounted at all times.
The fun thing was, no shell script file is needed. The command is in the plist, in ProgramArguments.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.common.mount</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hdiutil</string>
<string>attach</string>
<string>/Users/CommonVolume.sparseimage</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/Volumes/</string>
</array>
</dict>
</plist>