launchd StartOnMount doesn't behave as expected

dhom
New Contributor II

I'm trying to show a popup whenever a USB device is connected. There are a few suggestions to use a launchdaemon with StartOnMount, but this gets triggered every time a computer is unlocked from screensaver. Do you have any suggestions on how to approach this problem?

Sample code below:

<?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>com.company.popup</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/path/to/script.sh</string>
    </array>
    <key>StartOnMount</key>
    <true/>
</dict>
</plist>
#!/bin/bash
date >> /tmp/test.txt
1 ACCEPTED SOLUTION

howardgmac
New Contributor III

It may take some additional refinement but I think what you're trying to do is something like this:

<?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>com.company.popup</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/path/to/script.sh</string>
    </array>
    <key>WatchPaths</key>
      <array>
         <string>/Volumes/</string>
      </array>
   <key>KeepAlive</key>
      <false/>
</dict>
</plist>

So that anytime there is change the the Volumes list (like a USB drive mounted) the pop up would be fired. If you wanted it to only fire with USB devices then you would need to refine this to check for the presence of new USB devices, etc.

View solution in original post

4 REPLIES 4

mm2270
Legendary Contributor III

What is the script doing that gets triggered by the LaunchDaemon? Are you only sending the pop up message up if it sees an attached USB drive? I' thinking it might be possible to work around this within the script. Or, are you saying with a USB drive plugged in and the screensaver unlocked, the script fires, each time? If so, I would imagine in some way, the OS is remounting attached drives when the screen gets unlocked, although I don't know for sure. It does sound like that's the case though.

howardgmac
New Contributor III

It may take some additional refinement but I think what you're trying to do is something like this:

<?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>com.company.popup</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>/path/to/script.sh</string>
    </array>
    <key>WatchPaths</key>
      <array>
         <string>/Volumes/</string>
      </array>
   <key>KeepAlive</key>
      <false/>
</dict>
</plist>

So that anytime there is change the the Volumes list (like a USB drive mounted) the pop up would be fired. If you wanted it to only fire with USB devices then you would need to refine this to check for the presence of new USB devices, etc.

mm2270
Legendary Contributor III

I can confirm the behavior @dhom is describing. With a USB drive mounted and a LaunchAgent using StartOnMount to display a dialog, locking the screen and coming back later fires it. This seems to be because of an issue with Yosemite (not sure about 10.11) where locking the screen seems to incorrectly unmount the disks. I can tell this easily because I get a bunch of Notification Center messages explaining that the drive was not "Ejected Properly", which is BS, since all I did was lock my screen. See image below.

ce91e61fb1504f6ea87bb4522b34db66

So of course, the LaunchAgent gets fired again after unlocking because the USB drive remounts, firing the script. I chalk this up to bugs in the OS that Apple has yet to fix. There's no logical reason for the drives to be unmounted by simply locking the screen. I never saw this behavior under 10.9 or earlier.

dhom
New Contributor II

I ended up going with @howardgmac's suggestion of using WatchPaths. The script checks for new USB volumes and shows the popup as needed.