I have recently been playing with Plex on a Mac mini. The Mac mini is using a Network Attached Storage (NAS) device for storage of the media files. I am a bit of a power miser so had the Mac mini set for pretty aggressive power saving. I was going to watch my media files and my system was constantly coming up with no content found. Relaxing the power settings did not solve the problem for me unless it was for very extended periods. It did not take me long to find out that Mountain Lion has very aggressive sleep settings which whilst good also have dramatic impacts upon network attached storage – those network drives are essentially lost. So the question for me is how do you get OS X 10.8 to reconnect those network drives every time the Mac mini wakes.
I found a post here from The Glenbot on Sleep and Wake scripting for OSX. It has formed the basis for my attempts to solve my problem. I have taken some parts of that post and placed them here as a reminder for myself of what I have done and to help any others interested. I recommend you visit the site and read the full post and watch the embedded video.
Exerpt – The Glenbot
Head over to http://www.bernhard-baehr.de/ and download a copy of Sleepwatcher 2.2. The installation instructions are pretty simple and need to be done in terminal:
$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin
$ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8
If testing is needed run it in verbose mode:
$ /usr/local/sbin/sleepwatcher –verbose –sleep /path/to/your/sleepscript –wakeup /path/to/your/wakeupscript
Once satisfied , run it in daemon mode:
$ /usr/local/sbin/sleepwatcher -d –sleep /path/to/your/sleepscript –wakeup /path/to/your/wakeupscript
Sleepscript – Wakeupscript
Two files are needed for this:
- sleepscript – script that runs when the computer sleeps
- wakeupscript – script that runs when the computer wakes
The scripts can be placed anywhere but the standard place would be to place them in /Users/<username>/bin. The bin folder should contain:
Make sure to chmod the scripts to 775
Getting the script working on bootup is trivial. The package comes with an Apple plist file de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist that can be loaded with launchctl.
The steps ask you to create a ~/Library/LaunchAgents directory. This directory might already exist if you have installed something in that past that requires to be launched on bootup. It is a idiomatic directory that is used by launchctl.
Once you copy the plist file into ~/Library/LaunchAgents rename it to de.bernhard-baehr.sleepwatcher.plist then you can just run launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist to have it load on bootup.
NOTE!! You will need to change some of the parameters in the plist file to point to YOUR sleepwatch executable and scripts. Using my examples above it would look like:
<?xml version=”1.0″ encoding=”UTF-8″?>
Mounting Drives From Script
What was required next was the adding of code to the wakeupscript file to remount the network drives. Searching for this led me to this link at Mac OS X Hints. It has a number of scripts that could be used for sleepscripts and wakeupscripts. From this location I was able to identify how it would be possible to accomplish what I wanted. I combined that with information found here at HackMac to come up with my wakeupscript. The echo ‘sleep 10′ line is simply to add some time into the wakeup process so the network is back up.
echo ‘sleep 10′
osascript -e ‘tell app “Finder” to mount volume “afp://servername._afpovertcp._tcp.local/dir1″ ‘
osascript -e ‘tell app “Finder” to mount volume “afp://servername._afpovertcp._tcp.local/dir2″ ‘
osascript -e ‘tell app “Finder” to mount volume “afp://servername._afpovertcp._tcp.local/dir3″ ‘
osascript -e ‘tell app “Finder” to mount volume “afp://servername._afpovertcp._tcp.local/dir4″ ‘
To test this I have unmounted the drives put the Mac mini to sleep then woken it from sleep. On each test the drives appear again to be mounted. So here is hoping that it continues to work for me.