OS X: Auto-mount network drive upon server disconnect

I needed a solution [on Mac OS X] that would automatically reconnect my network drives from a file server when it disconnects and reconnects. There is indeed a method to connect network drives when OS X boots up (the instructions are below for this).  Unfortunately, this does not reconnect the network drives if the server itself goes offline and then comes back online.

So here is the scenario: I have a Plex server on an OSX virtual machine.  The “Movies” and “TV Shows”  folders are on another server and I have them mapped using smb in Finder:

Click finder > click “Go” in the menu bar (or press CMD+K) > then type in the server address: smb://server_ip
(in my case I type smb://serverbox/Movies, and smb://serverbox/TV (HD))

connect_to_server

This will mount your drives, but what this will not do is keep them mounted if you reboot your OSX machine OR if your file-server is rebooted.  We’ll start by talking about the first scenario: having your drives mount automatically when you reboot your OS X computer:

You can accomplish this by going to Apple Symbol (in top left of menu bar) then System Preferences.

system_pref

Click Users & Groups

users_and_groups

Click the User then Login Items then click the plus symbol and select the network folder.

login_items

This will mount your drives when the OS X machine is restarted.

Now how do we re-mount a drive if the datastore itself is rebooted?  There are a couple methods, but the method below is perhaps the easiest and not as difficult to learn as the other method which involves Terminal commands.

To remount network drives that were disconnected, you first need to create an Applescript.

Type Applescript Editor in your OS X spotlight search and open it.

Copy the following lines in the lines below into the applescript editor:

## Save this as an applescript application with the “always open” checkbox checked.
on idle
tell application “Finder”
    set isConnected to disk “Movies” exists — this checks to see if “Movies” exists (mounted) then sets a true/false value to the variable “isConnected”.
    set isConnected2 to disk “TV (HD)” exists
end tell
if isConnected = false then — this checks variable “isConnected” and if “false” then it tries to mount the volume given the server name (or ip address).
    try
    mount volume “Movies” on server “serverbox”
    end try
end if
if isConnected2 = false then
    try
    mount volume “TV (HD)” on server “serverbox”
    end try
end if
return 5 — this repeats the loop every 5 seconds; this value can be increased.
end idle
 

Save the applescript but make sure the “stay open” checkbox is checked.

What it does is check to see if the drives “Movies” and “TV (HD)” exist; if they do NOT (because the drive has disconnected), it will TRY to mount the volume.

When you save this, put it in your applications folder, and use the above tutorial to include it in your Login Items so it starts the application when your computer or OS X server turns on.

Leave a Reply

Your email address will not be published. Required fields are marked *