Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

~/Library/LaunchAgents plist runs manually but not automatically

Tags:

launchd

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
like image 915
user548071 Avatar asked Dec 20 '10 00:12

user548071


2 Answers

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.

like image 69
Alan W. Smith Avatar answered Oct 10 '22 23:10

Alan W. Smith


I just had a similar problem with automatically launching the services in ~/Library/LaunchAgents, but in my case NONE of the *.plist defined services got started.

The problem was obviously connected to the directory ~/Library/LaunchAgents and not the plist files itself. The solution was to reset the file permissions.

chmod 700 ~/Library/LaunchAgents.

Update for homebrew users: (2015-02-23)

Yesterday I just found LaunchRocket which is a Mac PreferencePane for managing services with launchd. It is homebrew aware ands adds a nice UI for managing launchd homebrew services.

This may not help you with incorrect user permissions but it is open source so you can fork the project and add the permission check as a feature.

like image 28
Gregory Igelmund Avatar answered Oct 10 '22 23:10

Gregory Igelmund