~/Library/LaunchAgents plist runs manually but not automatically

user548071 picture user548071 · Dec 20, 2010 · Viewed 20.9k times · Source

I am starting to work with launchd and want to set up a plist file such that whenever I insert an SD card into my Mac mini server (with Snow Leopard Server), I want a shell script to run (which should copy all the jpg files, rename them etc).

So, I created a plist file in the ~/Library/LaunchAgents (see below for its contents - it should be looking for changes to /Volumes) and I created a shell script which says "beep" - later it will do something more useful.

The plist file is registered with launchctl, and when I run it (launchctl start com.peters.runwhenSDmount), the computer says beep whenever a memory card is plugged in, and stays silent when there is no memory card. So, apparantly the plist does call the shell script, which subsequently checks if the specific SD card is there. I assume this also proves that there is no problem with permissions for the SD card.

But, it doesnt seem to run by itself??? Any idea why??


plist file: ~/Library/LaunchAgents/com.peters.runwhenSDmount.plist

<?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">
<dict>
    <key>Label</key>
    <string>com.peters.runwhenSDmount</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
    <string>/Users/peter/Library/Scripts/runwhenSDmount</string>
    </array>
    <key>ThrottleInterval</key>
    <integer>10</integer>
    <key>WatchPaths</key>
    <array>
    <string>/Volumes</string>
    </array>
</dict>
</plist>

shell script: ~/Library/Scripts/runwhenSDmount

#!/bin/bash
if [ -d "/Volumes/NIKON D40X" ]; then
    say beep
fi

Answer

Alan W. Smith picture Alan W. Smith · Jul 31, 2011

After you create a new plist in your ~/Library/LaunchAgents folder, you have to tell the launchd application about it. The two basic ways to do that are:

  1. Logout then log back in. - Every time you log in, launchd will scan the contents of your ~/Library/LaunchAgents folder and add any plist it finds there.

  2. Load the plist from a terminal command line with "launchctl". The syntax of the command is:

    launchctl load {Path-to-plist}
    

The launchctl command can also be used to stop launchd from using a plist. To do that, use:

launchctl unload {Path-to-plist}

The launchctl command is very useful when developing plists since it makes unloading/loading them between changes quick and easy. Once you have a plist working the way you like, the automatic login launchd loading can take over.