~/Library/LaunchAgents plist runs manually but not automatically

21,417

Solution 1

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.

Solution 2

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.

Solution 3

Two tools that help with the creation and management of launchd items are:

  1. LaunchControl - "LaunchControl is a fully-featured launchd(8) frontend allowing you to create, manage and debug system- and user services on your Mac."
  2. Lingon - "An easy to use yet powerful utility that runs things automatically on your Mac"

As a note: Brett Terpstra (who does a bunch of great Mac work on things like nvAlt) recently commented in his post "Triggering tasks remotely with notifyutil and launchd" that he used to use Lingon, but has been using LaunchControl more recently. Either of them is worth looking into.

Share:
21,417

Related videos on Youtube

user548071
Author by

user548071

Updated on July 09, 2022

Comments

  • user548071
    user548071 over 1 year

    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
    
  • anka
    anka about 11 years
    Another easy tool to load and unload your launch agents could be itunes.apple.com/at/app/launchdcontrol/id590231085?mt=12
  • Norman Gray
    Norman Gray over 9 years
    Though this is indeed sort-of obvious (and this advice helped me fix a problem, I think), I was unable to find chapter and verse stating this requirement
  • Jen Garcia
    Jen Garcia about 9 years
    Indeed, this is the first time I've seen anyone suggest resetting the permissions on ~/Library/LaunchAgents but it was exactly what I needed. high five
  • Yann Vernier
    Yann Vernier over 6 years
    I too am unable to find documentation, but can confirm I had to set user-only permissions on not only LaunchAgents but the plist within. There's not even a log message about the issue.

Related