Mac Mini Media Centre – Energy Settings and iTunes Library

With the hardware plugged in and my essential software and codecs installed I was going to crack on with Plex but I needed to sort out my iTunes library and also make a decision – to sleep or not to sleep.

iTunes Library
A quick recap on my hardware setup. I have purchased a Mac Mini and will be using it as a Media Centre device for under the TV. Connected to the Mini is a Drobo. The Drobo holds all my media keeping the Mac Mini virtually free of content. I also currently have an iMac and Macbook pro. My iTunes library was sitting on the iMac but I wanted to move it to the Drobo and access the content from the iMac or the Mac Mini.

Moving the iTunes library was fairly straightforward as I let iTunes manage my music. This is a setting in iTunes preferences which means iTunes looks after the naming and location of all my iTunes managed media. To move the content I followed an excellent guide at iLounge on Transferring your iTunes Library. As I had moved to the iTunes 9 Media layout all I had to do was change the media folder location in iTunes Advanced preferences, then goto File, Library, Organise Library and select Consolidate Library. This copied all the media from the iMac to the Drobo via the network share I had created. Once this was complete I checked that all files including podcasts, movies and mobile applications had been copied and that all my music was now referencing the Drobo – do this by right clicking on a track and selecting Get Info. At the bottom of the Summary tab a Where field details the location of the file.

Once confirmed I then removed all the media content from the iMac. Happy days, or so I thought.

Energy Settings
One of the choices to make with your Mini is whether to have it always on or to sleep when not in use. This will very much depend on what you want to do with the Mini. For me, even though I will be using it for remote access and for downloading media, I still want it to sleep when not in use. I waste enough electricity as it is without having a Mini and Drobo switched on 24/7. So the Mini will sleep when not in use. However, unless you have an Apple Airport Extreme or Time Capsule you won’t be able to Wake on Demand which is a feature that both these devices support for any Apple attached hardware. This means when I start my iMac and the Mini is asleep, my iTunes library isn’t available. When this happens, music won’t play and any downloads will also reside locally in a newly created iTunes library. Not good at all.

To get around this I’m making use of a little known free Mac tool called SleepWatcher. This is a command line tool that watches two scripts – .wakeup and .sleep and will run the .wakeup script when your machine wakes up and .sleep when your machine sleeps. My solution to the sleeping Mini was to have the commands in my .wakeup script to wake my Mini, create a mount point and then mount my music network share. iTunes would then work properly and my music would be available with downloads being placed on the Drobo.

So lets go through each of the steps and explain the scripts I now use. Firstly I downloaded and installed the SleepWatcher utility which comes in two parts. SleepWatcher.pkg installs the actual SleepWatcher command. SleepWatcher StartUpItem.pkg installs the daemon and also the sleep and wakeup scripts that are executed by the daemon. With both installed I was ready to create my wakeup script.

In TextMate I created a new file called .wakeup in my user directory i.e.

/users//.wakeup

so for me

/users/ian/.wakeup

Remember to make the script executable:

chmod 755 .wakeup

I wanted the file to wake the Mac Mini. To do this I installed the WakeOnLan command line utility in a scripts directory in my user folder. Using WOL command line allowed me to wake the Mini using the following command:

wol ip_address mac_address

where ip_address is the fixed IP address for the Mac Mini and the mac_address is the unique MAC address for the network adaptor in the Mini. To find the MAC address for a machine launch System Profiler (Apple Menu, About This Mac, More Info or Applications, Utilities and the System Profiler), select Network and then select the Ethernet Active Service. Scroll through the information displayed and under the Ethernet heading you will find your MAC Address. You could also download WakeOnLan which is a tool which will scan your network displaying connected machines and their MAC addresses. From the command line the wol command worked well but adding it to the .wakeup script did nothing. The Mini would resolutely stay asleep no matter what I did. Odd. I then created a shell script containing the wol command and ran the shell script which woke up the Mini. Odder. I then called the shell script from the .wakeup script…success!

So my .wakeup script as it stands is as follows:

#!/bin/sh
# ------------------------------
# Wakeup Script for SleepWatcher
# ------------------------------

# Wait for network connect
sleep 5 

# Wake Mini and Mount drive
/users/ian/scripts/mnt_mus.sh

The script sleeps for 5 seconds waiting for the network to be up and running and then launches the mnt_mus.sh script which wakes the Mini and then mounts the drive. The mnt_mus.sh looks like this:

#!/bin/sh
# ———————————————
# Wake Mini and Mount Script
# ———————————————

#Wake Mini
/users/ian/scripts/wol/wol ip_address mac_address
# Create Mount Point
mkdir /Volumes/music
# Wait for mini start
sleep 10
# Mount drive
/sbin/mount -t afp afp://username:password@mini.local/music /Volumes/music

So with SleepWatcher, WOL and the scripts above my Mini can sleep and when my iMac wakes, it will wake the Mini and mount the music network share. Happy days. A final step is to call the .wakeup script at logon/startup so that no matter what the event, the network share is mounted. This is easy to do. I copied the .wakeup script to a new file called login.command (creating a command file), made it executable, then added it to my Login Items in System Preferences -> Accounts pane. By making the shell script a command file it makes it accessible to the Login Items GUI. So when I start or wake my iMac the drive will be mounted and I can access my music.

Moving the iTunes Library Database
While the iTunes media has been moved the Library database was still on the iMac. Although it takes up only a few hundred MB I wanted to move it so I could share the library file with iTunes on the Mini. The database file comprises of an XML file which contains all the metadata for your library, a Genius file and the Album Art folder. To move the database I closed down iTunes, moved the iTunes folder to a folder on the Drobo and restarted iTunes with the OPT key held down. This prompts you to create a new library or choose an existing library. I chose the library via the network mount and after a few seconds iTunes was up and running.

Sharing the iTunes Library Database
The final step in the process was to share the iTunes library between the iMac and Mini. I thought this would be simple but it’s not so straight forward. iTunes was built on the assumption that one iTunes would access one library and that’s it. You could share a library between two different iTunes but you must make sure that only one iTunes is accessing the file at any one time or you risk corruption. I don’t like those odds!

I could solve it by putting checks and locks in place like don’t launch itunes on Mini if iMac iTunes is running and vice versa but I’m sure there would be some scenario that would undo me. Therefore I’ve taken the safer route of having the Mini point to a copy of the iTunes database which is synced to keep it up to date. To automate this I’ve relied on SleepWatcher again.

On the Mini I’ve installed SleepWatcher and the .wakeup script calls the following script:

#!/bin/sh
# ------------------------------
# iTunes database Sync
# ------------------------------

# Sync Folders
rsync -a /Volumes/Drobo/music/"iTunes Library Files" /Volumes/Drobo/music/"mini iTunes Library Files"

Using rsync I copy the iTunes database from the folder iTunes Library Files to a separate folder called mini iTunes Library Files. The advantage of rsync is that after the initial copy which mirrors the file and folder structure, only differences in files are copied making the sync quick and painless. Therefore every time the Mini wakes the folder is synced. I created a symbolic link on the Mini to match the networked folder seen elsewhere and bingo – my library was being shared and up to date. I changed the settings on the Mini so it doesn’t download podcasts and I’ll still manage my library on the iMac but read below to see why this may change. I thought that was it but I still had one more issue. iTunes only updates the library files on exit.

To make sure I sync the most up to date library I have created a .sleep file on the iMac and Mini that will close iTunes on the machine sleeping. That script can be seen below.

#!/bin/sh
# ---------------------
# Close iTunes on Sleep
# ---------------------

#Close iTunes
osascript <<< "tell application \"iTunes\" to quit"

It's not perfect but again it's working well for me at the moment and means both computers are sharing the same media files with two different databases. If all the above is a bit too much then there is software like SuperSync or myTuneSync which will keep separate libraries in sync

Future Options
The scripts above are very much a 'now' solution. In the future I plan to consolidate down to the Mini plus one other machine. If that machine turns out to be a laptop then the Mini will act as the master for iTunes and I'll rsync all my music between the Drobo and the laptop so that my music is with me at all times. Extreme perhaps but I do want to access my music wherever I am.

Closing
Hopefully this third post in the Mac Mini series here at DigitalOutbox, although optional, will give you some hints and tips in how to manage your iTunes library and how to best manage your Mini if you want to be a bit more energy efficient. If there is something glaringly wrong or a better way of achieving the above then please leave a comment or drop a mail as I'm sure there are better methods and this is a learning experience for me.

With the first three posts out of the way it's now time to install Plex, but thats for the next post. What a tease!