Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mvmanolov

macrumors 6502a
Original poster
Aug 27, 2013
858
5
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,
 
There's more than one way to do this but you can try an AppleScript together with a Calendar Alarm in Automator.

Info : Mount and unmount external drives easily via AppleScript - automating-with-calendar-in-mountain-lion-and-mavericks - Timed Jobs Using launchd

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 :(
 
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 :(

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 }'")
 

Attachments

  • Screen Shot 2014-10-31 at 03.52.28.png
    Screen Shot 2014-10-31 at 03.52.28.png
    173.4 KB · Views: 246
  • Screen Shot 2014-10-31 at 03.52.58.png
    Screen Shot 2014-10-31 at 03.52.58.png
    172.3 KB · Views: 224
Last edited:
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. :)
 
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. :)

Awesome! +1 :cool:
 
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
 
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.
And you're done. :)

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,
 
Last edited:
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

Yeah, textedit will work but you need to change the format to pure text before you save (cmd, shift, T short cut).

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

That should work.
 
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.

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,
 
By the way how could i learn how to do things like this myself? Any tips on where to start?

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
 
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.