Jump to content

Automounting a network volume on wake from sleep

Recommended Posts

On a recent show we discussed how to set up network shares to mount on startup using the Accounts-->Login Items System preferences. Well listener Andrew discovered that when his Mac went to sleep the volumes would unmount and then not reconnect after waking from sleep. Well we did some looking around and came up with this solution:


NOTE: I have not personally tested this solution. I am also sure that there are some things that could be improved or expanded, so feel free to provide feedback and comments. Use at your own risk.


Step 1:

Determine the address of your server volume. For example, you can use the IP address of your other computer, or you can use that computer's name. Let's say you're going to use the computer's IP address (only do this if you've assigned a static IP address to that computer, otherwise use the computer name) you would use something like this: "cifs://" where MyHardDrive is the name of the shared drive on your other computer.


Step 2:

Download SleepWatcher from this website:




Mount the DMG file, then run both PKG files.


You might want to read the Read Me file as well to get some general information.


You can now unmount the DMG.


Step 3:

Create an AppleScript for mounting your drive by doing the following.


Open Script Editor.app (found in Applications -> AppleScript)


Type the following:


mount volume "ADDRESS FROM STEP 1"

end try



Click on the "Compile" button to be sure you don't have any errors. Once that is done, go to File -> Save As


Name the file ".mount" (without the quotations) and save it as a Script in your home directory.


Quite Script Editor


You have just created a script that mounts your drive (note that you can add multiple drives here, just add multiple "mount" lines with different addresses).


Step 4:

Now we need to create two files that tell the SleepWatcher program what to do when the computer goes to sleep and wakes up. I found that I can only get the server volumes to remount properly if I first disconnect them before going to sleep. To do this, open up Terminal (Applications -> Utilities -> Terminal.app)


Type the following command:


nano -w ~/.sleep


Once you are in here, you need to type the following text:


umount /Volumes/MyHardDrive
rmdir /Volumes/MyHardDrive


Make sure to replace "MyHardDrive" with your drive's name (you can do multiple volumes here)


To exit, hit Ctrl+X, then hit the "Y" key.


Now we change the permissions on this file so that it can be executed. Type the following in Terminal:


chmod 744 ~/.sleep


Now we have to create a file that tells the computer to run the Mount script when the computer wakes up. I got this code from this website but modified it a bit:



At the terminal prompt, type the following:


nano -w ~/.wakeup


Now enter the following text (should be able to copy and paste it):


status_wifi=$(ifconfig en1 | grep status | awk '{print $6}') 

#Check if wifi is up
if [ "$status_wifi" == "inactive" ]
 echo "No Wifi"
 echo "Got Wifi"
 SSID=$(system_profiler SPAirPortDataType | awk -F": " '/Current Wireless Network/{print $2}')
 echo $SSID

 if [ "$SSID" = "MY_SSID" ] #replace MY_SSID with your SSID
	 umount /Volumes/MyHardDrive
	 rmdir /Volumes/MyHardDrive


This code will check whether you are connected to your WiFi network using your wi-fi SSID (replace "My_SSID" on line 14 with your SSID) . On the third line where it says "sleep 15" this means that it will wait 15 seconds before checking the network (this should give the WiFi enough time to connect itself but if yours takes longer, just increase this value appropriately).


Again, hit Ctrl+X to exit and hit the "Y" key to save your changes. Change the permissions again by typing this:


chmod 744 ~/.wakeup


Step 5:

Exit Terminal and give it a shot (put your computer to sleep and then wake it up and see what happens).


This is a simplified approach where you have to type in all the addresses of volumes to attach and unmount but it works fine for me with two volumes I want to attach.

Share this post

Link to post
Share on other sites

I have a few questions on this code as I try to keep my readynas Duo NAS mounted after I wake from sleep. Sorry - a bit new at the terminal thing:


1. My applescript code works well when I run it manually. I don't, however, see any mention of the .mount script in the wakeup - how does it know to run it? I do see code for umount (which I assume UNmounts the drive) - the same as in the sleep script. Please explain.


2. In the .sleep and .wakeup, do I put the exact same drive name as I did in the apple script (in applescript it was afp://, so in the .wakeup code I wrote /afp://


Thank you!

Share this post

Link to post
Share on other sites

  • Recently Browsing

    No registered users viewing this page.

  • Who's Online (See full list)

    There are no registered users currently online