USB drive (timed) automatic mounting/dismounting script.

Discussion in 'Mac Programming' started by mvmanolov, Oct 30, 2014.

  1. mvmanolov macrumors 6502a

    Joined:
    Aug 27, 2013
    #1
    Hi all, and thank you for taking the time to read this!

    What i want to be able to accomplish is:

    1) have OS X automatically mount a usb attached drive (which will never be physically disconnected) at 1am every day.

    2) have OS X automatically unmount the drive at 2am every day.

    Any help with making this happen would be phenomenal!

    Thank you!

    M,
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  3. mvmanolov thread starter macrumors 6502a

    Joined:
    Aug 27, 2013
    #3
    Thank you for this, I have never don't any programming, so this looks quite complicated (excepting the calendar alarm). i may try my hand at it. My main challenge is that while the first link offered some examples, i don't actually know what i need to change in order to reflect my own environment :(
     
  4. kryten2, Oct 30, 2014
    Last edited: Oct 30, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Use the script from nickv2002 from the first link and change diskname in this statement to the name of your USB drive as it shows up under Devices in the Finder :

    Code:
    set diskName to "diskname"
    Also change this :

    Code:
    awk '{ print substr($0,69,9) }'
    with a cleaner

    Code:
    awk '{ print $NF }'
    Or this :

    Code:
    set deviceLine to (do shell script "diskutil list | grep \"" & diskName & "\" | awk '{ print substr($0,69,9) }'")
    with this :

    Code:
    set deviceLine to (do shell script "diskutil list | awk '/" & diskName & "/  { print $NF }'")
     

    Attached Files:

  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    Alternative way to do it with diskutil. Below I have used a place holder, "VOLUME NAME" which you will need to edit with your volume name.


    Schedule mounting at 13:00.

    Copy and save the following as: com.yourname.timed_mounting.plist in ~/Library/LaunchAgents/.

    Code:
    <?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.yourname.timed_mounting</string>
            <key>ProgramArguments</key>
            <array>
                    <string>diskutil</string>
                    <string>mount</string>
                    <string>VOLUME NAME</string>
            </array>
            <key>StartCalendarInterval</key>
            <dict>
                    <key>Hour</key>
                    <integer>13</integer>
                    <key>Minute</key>
                    <integer>0</integer>
            </dict>
    </dict>
    </plist>
    
    Schedule unmounting at 14:00

    Copy and save the following as: com.yourname.timed_unmounting.plist in ~/Library/LaunchAgents/.

    Code:
    <!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.yourname.timed_unmounting</string>
            <key>ProgramArguments</key>
            <array>
                    <string>diskutil</string>
                    <string>unmount</string>
                    <string>VOLUME NAME</string>
            </array>
            <key>StartCalendarInterval</key>
            <dict>
                    <key>Hour</key>
                    <integer>14</integer>
                    <key>Minute</key>
                    <integer>0</integer>        </dict>
    </dict>
    </plist>
    
    Now, everything should be done. Log out and log in again and you have your scheduled mount/unmount in place. One thing to remember though, if you eject the drive from Finder it will both unmount and detatch it. For this to work your USB key needs to be attached, so if you want to unmount it manually, do: diskutil unmount "VOLUME NAME" in Terminal or diskutil will not find the drive.


    But, in addition you actually need a 3rd Plist file to automatically unmount the disk every time you log in since OS X will mount it automatically. If you happen to log in between 1 and 2 am though, well you'll need to write a script that checks if this is the case an only perform the unmounting if that isn't the case. :)

    Code:
    <?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.yourname.unmount_at_load</string>
            <key>ProgramArguments</key>
            <array>
                    <string>~/Scripts/unmount_maybe.sh</string>
            </array>
            <key>RunAtLoad</key>
            <true/>
    </dict>
    </plist>
    
    The script needs to be placed in a folder called ~/Scripts which is in the root of your home directory and be called unmount_maybe.sh.

    Code:
    #!/bin/bash
    
    VOLUME_NAME="VOLUME NAME"
    TIME=$(date "+%H")
    
    if [ "$TIME" -lt 13 ] || [ "$TIME" -ge 14 ]
    then
        diskutil unmount "$VOLUME_NAME"
    fi
    
    You need to make this script executable with: chmod +x unmount_maybe.sh

    And you're done. :)
     
  6. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #6
    Awesome! +1 :cool:
     
  7. mvmanolov thread starter macrumors 6502a

    Joined:
    Aug 27, 2013
    #7
    Thank you both so much!

    This is absolutely amazing... I really appreciate your generous help!

    i'll try it all out tonight and report back as to how it works.

    M
     
  8. mvmanolov, Oct 31, 2014
    Last edited: Oct 31, 2014

    mvmanolov thread starter macrumors 6502a

    Joined:
    Aug 27, 2013
    #8
    So i tried to test it. but i am not sure of two things.

    1) how should i make the file (what i did was paste what you said in textedit and then manually renamed it to plist

    2) Also how can i change the time of mount unmount. i assumed that changing the "13"/"14" value changes the time correspondingly so since i wanted to run the unmount as a test at 8:20pm. i just changed "14" to 20. and the 0 minute value to 20

    this did not work...
    Obviously i'm doing something wrong, any suggestions.

    Thanks for the help again!
    M,
     
  9. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #9
    Yeah, textedit will work but you need to change the format to pure text before you save (cmd, shift, T short cut).

    That should work.
     
  10. mvmanolov thread starter macrumors 6502a

    Joined:
    Aug 27, 2013
    #10
    Success!
    Thank you again for this! It works like a charm!

    By the way how could i learn how to do things like this myself? Any tips on where to start?

    M,
     
  11. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #11
    Take a look at Automator, that's probably a good place to start. You can incorporate scripts there as well, and there's a lot to choose from out of the box, hard to give a general advice really. There's also some books on automation, scripting and system administration for OS X available. Apple has documentation as well that covers things like shell script if you're interested in the Unix side of things.

    http://www.macosxautomation.com/index.html
     
  12. mvmanolov thread starter macrumors 6502a

    Joined:
    Aug 27, 2013
    #12
    Thanks for this. I'm now using automation for some small things, but nothing to complicated.

    I want to try and setup off site backup that is automated. so i'll see how i can make that work with Automator.
     

Share This Page