How-To: Expand Wake On Demand Support Under OS X 10.6

Recently, I’ve been migrating the functionality of my old Ubuntu Linux server to my wife’s old iMac. Since a big part of the reason to decommission my old Linux PC was to reduce my total power consumption, I wanted to fully utilize Snow Leopard’s Wake On Demand functionality with as many services as possible.

Wake On Demand is a relatively new feature that arrived with Snow Leopard. It allows your Mac to be put into sleep mode and then be woken up on demand when one of its services is required. This feature requires a compatible Apple AirPort Base Station (or Time Capsule) and OS X 10.6 running on the Mac. Most of the standard system services (File Sharing, Screen Sharing, Scanner & Printer Sharing etc.) will work automagically with this setup, but custom services such a my subversion and the built-in web sharing do not. However it’s not hard to make these services compatible with Wake On Demand with only a little bit of work to set it up. Here’s how I did it.

Bonjour

For the Wake On Demand functionality to work properly, any service being hosted by a Mac must be registered with Bonjour, Apple’s zero config service discovery protocol. This is how the Airport Base Station knows when its appropriate to wake up a sleeping Mac. To assist with getting a complete picture, I’d recommend downloading a great free utility called Bonjour Browser. This application will show you all the services currently being broadcast by Bonjour on your network. Any service displayed here will be automatically enabled to use Wake On Demand by your AirPort base station. Most of the server software components such as File Sharing will register with Bonjour itself, however for the ones that don’t, we need to do it on their behalf.

Luckily this is easy, as OS X comes with a command line utility called dns-sd, the DNS Service Discovery tool. This ties directly in with the Bonjour system and with it we can register services with Bonjour. Lets use subversion as our example. Subversion’s TCP port is usually 3690, so assuming you have a standard setup, we can register subversion with Bonjour with:

dns-sd -R "SVN" _svn._tcp. . 3690 pdl=application/svnserve

If we run this command manually in Terminal.app, we can quickly verify that our Bonjour broadcast was successful by using Bonjour Browser. The service will remain registered with Bonjour for as long as the dns-sd command is still running; as soon as we press Control+C to kill the command, we can see in Bonjour Browser that our service disappears.

Of course it’s not terribly useful if we need to manually run a command every time our computer reboots (even if Macs don’t need to be rebooted that often). To make the Bonjour registration occur automatically at system startup we need to launch our command using the launchd subsystem, which OS X uses for automatically starting background services. The easiest way to configure this to run automatically via launchd is by using Lingon. You can grab the latest version from Sourceforge. For more details on using Lingon see this previous article I put together, but for this task we just need to add a new User Daemon.

Run Lingon, click the New button and select User Daemons.

Fill out the dialog as follows:
Name: org.subversion.bonjour
What: dns-sd -R "SVN" _svn._tcp. . 3690 pdl=application/svnserve

Be sure to tick the “Keep it running all the time” and “Run it when it is loaded by the system” checkboxes. Click the save button and restart your Mac. If everything has been done right, you should be able to see this instance of dns-sd in Activity Monitor when viewing All Processes.

Rinse and Repeat

The process described above can also be used for any other service, such as for the Web Sharing service (port 80) or any other custom service that you’ve setup. Simply replace the references to the application protocol (ie.┬ásvn) and the applicable port number and create a new launchd item for each one. This isn’t the most ideal solution, as each new instance of dns-sd takes about 380KB of memory, however if you’re running a number of these extra services I would assume that you have at least 2GB of ram and the impact of 380KB is negligible. Each instance of dns-sd also uses a negligible amount of CPU processing time so it won’t get in the way of other processes. Of course the perfect ideal solution would be that the server software you’re running registers itself with Bonjour, but if that was the case you wouldn’t need to be reading this article.

Final Result

With everything set up correctly, you should now be able to replicate the following test:

  • Put your Mac to sleep
  • From another device on your network access your custom service(s)
  • Rejoice with a warm fuzzy feeling as you realize your electricity bill will be lower and your carbon footprint is a smaller than it was yesterday.

I hope this has helped someone else achieve on-demand server bliss, with the peace of mind of minimal power consumption like it has for me. Do you have an even better way of doing it?

loading

Comments have been disabled for this post