[launchd-dev] Launch shell script when volume is mounted
Luke Scott
luke at cywh.com
Wed Apr 20 09:35:47 PDT 2011
Assuming "df" follows the same rules as mount (last mounted is last in the
list), this may be cleaner:
#!/bin/bash
volume=`df`
volume=${volume##*%}
echo $volume
Luke Scott
On 4/19/11 1:57 PM, "Matt Calthrop" <matt at calthrop.com> wrote:
>Thanks Michael.
>
>I can get it to trap the mount event and execute a script no problem.
>
>But what I couldn't do was to pass the name of the volume just mounted
>in to the shell script being executed.
>
>However: I found a hack that works - for now!
>
>By observation, running the mount command to show the mounted
>filesystems lists the filesystems in the order in which they were
>mounted - so the most recently mounted FS is at the end of the list.
>
>An example from my system, just after I mounted an SD card from my camera:
>
>/dev/disk0s2 on / (hfs, local, journaled)
>devfs on /dev (devfs, local, nobrowse)
>map -hosts on /net (autofs, nosuid, automounted, nobrowse)
>map auto_home on /home (autofs, automounted, nobrowse)
>/dev/disk1s1 on /Volumes/NO NAME (msdos, local, nodev, nosuid, noowners)
>
>So in my shell script that runs whenever a filesystem is mounted, it
>should have just been a matter of getting the last line of the output
>from the mount command, and extracting the 3rd parameter on the
>line... except that I've got an SD card that has been given the name
>"NO NAME", with a space in it (as you can see above). Grmbl.
>
>A bit of interwebs research revealed that bash actually has regex
>capabilities built in.
>
>So here's what I ended up with to extract the name of the volume:
>
>LAST_MOUNTED=$(mount | tail -1)
>REGEX="(/Volumes/)(.*)( \()"
>[[ $LAST_MOUNTED =~ $REGEX ]] && {
> NEW_VOLUME=${BASH_REMATCH[2]}
>} || {
> exit
>}
># followed by code to do my stuff with the volume just mounted
>
>So it's not entirely robust, but it works.
>
>BTW, you mentioned that you had tried duplicating the
>com.apple.backupd-attach.plist file, and modifying it to suit your
>needs, but that it didn't work - I assume you used launchctl to load
>the new plist file?
>
>Matt
>
>On 19 April 2011 09:20, Michael_google gmail_Gersten
><keybounce at gmail.com> wrote:
>>> I want to run a shell script whenever a volume is mounted and the
>>> shell script needs to somehow know what the name of the volume is that
>>> has just been mounted.
>>
>> The only trick I've found so far for volume mounts is catching the
>> call to backupd-helper.
>>
>> cat /System/Library/LaunchDaemons/com.apple.backupd-attach.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">
>> <plist version="1.0">
>> <dict>
>> <key>Label</key>
>> <string>com.apple.backupd-attach</string>
>> <key>ProgramArguments</key>
>> <array>
>>
>><string>/System/Library/CoreServices/backupd.bundle/Contents/Resources/ba
>>ckupd-helper</string>
>> <string>-attach</string>
>> </array>
>> <key>StartOnMount</key>
>> <true/>
>> <key>RunAtLoad</key>
>> <false/>
>> <key>KeepAlive</key>
>> <false/>
>> </dict>
>> </plist>
>>
>> Basically, there's a program that already runs on every attach. You
>> can replace it with a shell script.
>>
>> I have NOT been able to duplicate this behavior with anything else --
>> even trying to make a copy of that plist with a different name calling
>> my script did not work.
>>
>> A bit of a warning: It will trigger on every .dmg mount. Every time
>> you run a program off a compressed image (if you are trying to save
>> space), etc.
>>
>> And no, I can't figure out how to get the name of the mounted drive.
>> Equally, this setup does not have backup actually run and make a
>> backup when you connect a drive.
>>
>_______________________________________________
>launchd-dev mailing list
>launchd-dev at lists.macosforge.org
>http://lists.macosforge.org/mailman/listinfo.cgi/launchd-dev
More information about the launchd-dev
mailing list