Jump to content

macOS users - AutoMount your Shares

233 posts in this topic Last Reply

Recommended Posts

**** UPDATE ****


macOS 10.12 Sierra breaks this script. I've developed an application to get around this issue. Available here:





AutoMounter is a sleek and powerful menu item designed to automatically mount your network shares.


- Supports SMB / AFP / NFS network shares

- Supports multiple NAS appliances / servers

- Server discovery

- Persistent auto mounting of shares, when the server is reachable

- Match a server to a WiFi Name (SSID)

- Reacts to changes of network

- Optionally send a Wake On LAN packet to a server when the network changes

- Easy to use, unobtrusive, menu bar item

- Optionally hide shares from Desktop

- Resolves macOS Sierra authentication issue



Thought I'd share my quick and simple bash script (with the help from Stokkes) for mounting AFP/SMB shares. Set your preferences, add the shares, and it will ping the server. If it is available then it will mount the shares. There is also an option to load iTunes at the same time.


Now works with Lion.


**NEW** Added functionality to wake the server via WOL **NEW**

28/12/2012 - Added the ability to call "wake" at the end of the script to wake the server, no matter which time it is. eg: /scripts/mount.sh wake


You must have wolcmd (included) in the same folder as the script. This is a definable directory. If the server is unavailable via ping, WAKE is set to TRUE, and the time is within the hours specified, then the script will attempt wake on lan. You must have your server MAC address and subnet in the options. This is all shown in the script.


Now if the server is unavailable it will exit iTunes. I found iTunes to be unstable if it can't resolve links to its music. You can set this in the script


Step 1: Where to put mount.sh & com.media.mount.plist


- Create a folder called "Scripts" on the root of your hard drive. This could be /Macintosh HD/Scripts.

- Copy mount.sh to this folder.

- Copy com.media.mount.plist to your user *Username*/Library/LaunchAgents folder. If the folder doesn't exist, create it.


Step 2: Editing the mount script


- Open mount.sh in text edit. Read the notes at the top of the script.

- Enter your username and password inside the quotation marks where shown. Read the notes in the file if your password has symbols (eg @, as you'll need to url encode them). Site is given in the script.

- Enter your server IP address (eg "") and hostname (eg "tower") where shown. If your nas has a dynamic IP address (DHCP), enter the hostname instead of the IP Address.

- Leave Mount point alone, unless you want the shares to mount somewhere other than the default /Volumes

- Enter TRUE or FALSE if you want iTunes to open or not.

- Enter the protocol you want to use, either smb or afp.

- Enter the shares you want to mount. The shares must be space seperated (eg "TV Movies HD%20Movies". If the share name has a space in it, replace the space with %20 (eg "TV%20Shows").


Step 3: Enable script


- Double check the settings are correct, and make sure the mount.sh, and com.media.mount.plist files are in the correct place.

- Open Terminal (Applications/Utilities/Terminal). The sudo command will need your administrator password. Type:

                  - sudo chown root:staff /Scripts/mount.sh

                  - sudo chmod 775 /Scripts/mount.sh

- Restart your computer. It should mount your shares on login, and check if they're mounted every one minute.


If it's not working, double check permissions and settings. Try running the shell script by opening Terminal and typing:


- /Scripts/mount.sh

- Press enter


If that works, then check the status of the launchd agent by going into Terminal and typing


- launchctl list

- This will display the loaded launch daemons. If there is a 1 next to the com.media.mount.plist file, make sure the script is located on your boot volume, "/Scripts/mount.sh". If it's not in the list at all, make sure that the plist in your user Library folder, in a folder called "LaunchAgents".


Hope this is useful for someone!


Share this post

Link to post

Cheers! I've found a solution for quitting applications by their real names via terminal (not process ID, which tends to change often), and it will prompt to save documents first, or exit quietly. Unfortunately doesn't work with iTunes, as iTunesHelper daemon conflicts with the name.


If you need this, let me know.


You can also decide to umount Volumes if there server isn't available, where the else statement is. In the else statement (at the bottom) remove exit 0 and add umount /Volumes/Music etc.

Share this post

Link to post

I'll take a look at it when I get home this evening.


I've been meaning to make a script that'll run on a set schedule (read: often) to make sure my volumes are still mapped. I often have an issue in Plex where the volumes get unmounted (either the Mini goes to sleep or something else happens) and I can no longer stream my media. I'm then forced to vnc into the mini and remap the drives manually.. PITA.


So if this does what I've ben meaning to do, then fantastic.

Share this post

Link to post

Exactly the reason why I made this script  ;)  Also doesn't try and open the Volume in a window, like if you add the mounts to your login items. They just mount quietly in the background, and only if they're not mounted already and the server is on.


Very useful if you run plex, or if you have iTunes running all the time, and your library is sitting on the shares.


It runs every minute, so you're seldom left without your volumes mounted.


Also, remember to put in the LaunchAgents folder in your user folder. If you use the main Library folder, it won't work.

Share this post

Link to post

Few potential problems I've seen with the script. I'm working on a slight modification.


Long story short, there's a possibility that the shares mount correctly but are inaccessible because they are mounted by "root"

Share this post

Link to post

I'm not having those issues. You need to run them from your user folder launch agents. If you run from the main Library Folder, it will run as root. There's no way around this as far as I could see.

Share this post

Link to post



I've actually written a quasi Microsoft DFS mounting script (seeing as it OSX doesn't have built-in DFS support) and learnt a lot about mounting shares from the command line.


The password is truly a bugger, especially with symbols, etc. For example, the character "!" is a valid URL character and doesn't change during the url encode process, but wont' be accepted on the command line (have to change to %21). Very weird and frustrating :)



Share this post

Link to post

Oye... I've been in IT for about 16 years now. I do mostly management now, but still dabble in scripting, small-time programming.

Share this post

Link to post

Only been in the scene for a couple of years now. Continual effort to keep learning, it's hard work!


Have you tested the script yet? Just checked it out, something fishy with the loop for shares. It's creating folders called "echo" "if" etc, under /.

Share this post

Link to post

Yes I did and it works fine.. It's creating a folder called "echo" and "if"?




Can you post your variable SHARES= & ROOT= lines?


Share this post

Link to post

Root is same "/Volumes" and shares I've just been testing with "Music Downloads"


I'm actually getting parse errors on the comments. Something is up with the way my machine handles bash scripts...


What version of OS X are you using?



Share this post

Link to post

No worries! Stokkes did a better job though...


Did you manage to get Stokkes script to work? I tried again, but have issues running it on 10.6.6 client and server. Very strange  ???

Share this post

Link to post

Works fine here (10.6.6) using the hostname. Don's know why, but I couldn't get it to work using the server's IP.


edit: strange, on my Plex Mac mini (running 10.6.4) it's just the other way around, hostname doesn't work and IP does... I have some trouble to get it to mount my "TV Shows" user share. TV+Shows should work?

Share this post

Link to post

I rewrote my script with some of Stokkes code, added in option for AFP mounts, and now spaces should work. Read notes in file first.


File re-uploaded in first post.

Share this post

Link to post

Thanks again!


It works fine if I don't include a share with a space. Including TV%20Shows results in the following:

line 47: TV%20Shows: command not found

and neither of the shares (on with, one without a space) specified will mount.


Share this post

Link to post

Which script are you using? Can you attach your modified script (sans username/password of course) so I can check it out?


I've got a few shares with spaces, works sweet with me.



Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.