2

I'm wondering where's a good place to start for this problem. I believe it might be achieved using apple script but admittedly I don't know its capabilities.

Scenario: I have a NAS which I log in to using Finder. Once logged in there are two folders on the NAS, when I click one of the folders "SNAS" it mounts it on root (i.e., if I CMD+UP ARROW to the top, it's listed next to other drives). I then drag it into my favourites list.

When the Mac is rebooted the connection and favourite is lost and I have to repeat the process.

I want a script to run automatically on each boot, and then periodically (say, every 30 seconds or so). The script should check if the NAS is logged in, if not, log in to it. Check if the folder/drive is mounted, if not, mount it. Then check if it's on the list of favourites, and if not, favourite it.

In effect automatically logging in, mounting, and favouriting the NAS folder/drive each time.

Happy to do the leg work on the script but if anyone has a suggestion for where to start/language to use/or even possibly a dedicated application that would be great as I can't find anything for the niche request on searches

I've attached a run of images showing the current steps being taken.

OS details: macOS Sierra 10.12.1 (16B2555)

Steps needing automated

1

You can do this in the shell.

Launch a Terminal, then run:

mount

Find the line with your volume, and grab the first part up until the space. It should look something like:

//warren@10.0.15.2/Shared%20Network%20Volume

Thats username@IP_OF_NAS/VolumeName

Now here is the script, save it as "remount.sh"

#!/bin/bash

date
mounted=$(/sbin/mount | grep -c "VOLUMENAME")
if [ $mounted -eq 0 ]; then
   ping -q -c 2 -t 1 IP_ADDRESS > /dev/null 2>&1
   if [ $? -eq 0 ]; then
      if [ ! -d "/Volumes/VOLUMENAME" ]; then
         mkdir "/Volumes/VOLUMENAME"
         chown LOCALLOGIN:staff "/Volumes/VOLUMENAME"
      fi
     su LOCALLOGIN -c "/sbin/mount -t afp afp://YOURLOGIN:PASSWORD@IP_ADDRESS/VOLUMENAME /Volumes/VOLUMENAME"
   fi
else
   echo "Already mounted"

fi

You need to change the variables in all CAPS above.

YOURLOGIN (Your login on the NAS)
PASSWORD (Your password on the NAS)
LOCALLOGIN (Your local login on the MAC machine)
IP_ADDRESS (the IP ADDRESS of the NAS)
VOLUMENAME (The Volume Name on the NAS)

Unmount the network volume that you previously mounted, then run:

chmod 755 remount.sh
sudo ./remount.sh

Make sure this mounts the volume OK. It worked fine on my MacBook running Sierra against an Apple Time Capsule.

Now if all is well, we add it to cron to run it as root (sudo) every 15 minutes.

sudo crontab -e

Add something like this (season to taste):

*/15 * * * * /Users/warren/remount.sh >> /Users/warren/remount.log 2>&1

You'll note I saved the script in my home directory. If you are tidy, you probably don't want to save it there.

Dismount the volume, and wait 15 minutes and see if it reappears. If not, look at the log file to see whats up.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .