You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

launchd: launch on mount of SPECIFIC volume?

I'm trying to set up a launchd agent to run when a particular volume is mounted, as determined by the volume name. I've found a posting on this board saying it's possible, but without any further information. The man page for launchd.plist seems to suggest that this functionality is not available in the launchd framework.

Just thought I'd ask here in case someone knows the answer. It seems like this is a pretty basic, obvious function you would want from launchd, so I'm assuming it's just a failure of my research skills to figure out how to do it!

Thanks in advance to anyone who answers.
-dave

MacBook Pro, Mac OS X (10.5.4)

Posted on Aug 22, 2008 7:55 AM

Reply
15 replies

Aug 22, 2008 12:58 PM in response to omgoleus

one way to deal with this is to attach a folder action to /Volumes and make the script that it runs check the names of added volumes and if it contains the name you want execute the command you want it to execute. of course folder actions use launchd with "WatchPaths" so this is in effect the same thing but it's the only way I know how to check only the names of added volumes as opposed to all mounted volumes.

Aug 22, 2008 8:13 AM in response to omgoleus

cool question!

the easiest way is:
download lingon:
http://switch.dl.sourceforge.net/sourceforge/lingon/Lingon-2.1.zip
open it
create a new plist and select "run it every time a volume is mounted"
Like argument program plist select a script (i suggest save the script inside your volume, in this way when you mount a different disk, you have simply log error in system.log)

at the moment, no more ideas.

Aug 22, 2008 10:13 AM in response to omgoleus

The StartOnMount key only accepts a Boolean, so it's going to run every time ANY volume is mounted. You'd have to write your script to examine the list of volumes and decide whether or not it needed to run at that point.

You might also be able to do this with a watch path, but volume names are not guaranteed to be unique, so that might have some undesirable side-effects, too.

Aug 22, 2008 12:06 PM in response to orangekay

The watch path could work fine, but it appears that using the watch path implies KeepAlive True. That is to say, you can't have it run ONLY when the path first appears; as long as the path exists, launchd will keep the job alive.

Frankly this is kind of a stupid limitation, but it's easy for me to say, I didn't have to write the thing...

Lots of google searching suggests to me that as of 10.5.4, if you want to do this you have to write something into your script to round out the limited intelligence of launchd. I'm disappointed, Apple's "Getting started with launchd" document is so boastful! 🙂

Thanks for the suggestions, everyone.
-dave

Aug 22, 2008 1:46 PM in response to V.K.

So if launchd is configured as such;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.apple.WatchForFlashDriveGPSL</string>
<key>ProgramArguments</key>
<array>
<string>/Users/kkaiser/Desktop/AutoFlash.scpt</string>
</array>
<key>StartOnMount</key>
<true/>
</dict>
</plist>

It should run AutoFlash.scpt every time any volume including a flash drive is attached to the machine. Only it doesn't seem to work, anyone have an idea on this?

The following script when run outside of launchd works just like it should. Which in this case is just to launch MacScript.app and all that does right now is open the flash drive.

AutoFlash.scpt;

property flashState : 2
set triggerDisk to "GPSL"
set script2Run to (path to desktop as Unicode text) & "MacScript.app"
tell application "Finder"
if disk triggerDisk exists then --The file is found, therefore, the flash drive is present
if flashState = 2 then --This indicates that MacScript.scpt needs to be called
run script file script2Run
set flashState to 0 --After the script has been run, change the flashState property
end if
else --This is ONLY accessed when the disk no longer exists, therefore, the flash drive is NOT present
if flashState = 0 then --Since the drive is no longer present, the value of flashState must be re-set to 2 in order to prepare for the next hookup
set flashState to 2
end if
end if
end tell

Aug 23, 2008 12:53 AM in response to orangekay

Sorry, the QueueDirectories type keeps the job alive. The problem with WatchPaths appears to be that inserting a volume doesn't count as modifying a file, so it doesn't activate the job.

To be more specific, I set up WatchPaths with the path to the volume name I'm interested in, something like /Volumes/SANDISK/. This failed to cause the job to run when the SANDISK drive was connected.

If you know a way to get WatchPaths to respond to inserting a specific drive that would be great...

thanks
-dave

Aug 23, 2008 8:17 AM in response to omgoleus

This is the same idea suggested by Federico in the first response in this thread. didn't you read it?
It might be good enough for your purposes but technically speaking it's not a perfect solution because if you attach another drive while the drive with your script is already attached, the script will execute again even though it shouldn't. if that doesn't bother you then fine. otherwise, the folder action trick I mentioned works completely cleanly.

Aug 25, 2008 7:59 AM in response to omgoleus

omgoleus wrote:
There is no real solution,

why do you say that? the folder action method works perfectly.

but the workaround in the first reply is adequate for my purposes: use launchd to set up an agent to run on mount of a volume, and point it to a script which is on the volume in question.

launchd: launch on mount of SPECIFIC volume?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.