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

21 Comments

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?

21 Comments

Ralph

Is there a way to wake up a computer to run a scheduled application?
I am using CCC to do scheduled backups.
I set CCC to backup in the middle of the night, (when everyone including my iMac is sleeping). Because the machine is asleep, the backup does not start until I “wake up” the machine during the day
Is there a way to wake up the iMac in the middle of the night, enter the user password, run the backup, and go back to sleep when done?

Brian

Is there any way to execute the “wake” command from an Applescript? I often send files from my MacBook to my iMac using a Finder Service that I wrote. If my iMac is sleeping though, I have to take a first step of waking it up. It would be great if I could roll the wakeup into my script.

David Page

DNS also has the ability to broadcast bonjour services and the wide area bonjour feature of DNS in server 10.6 may help. Bonjour is multicast DNS and its the same system that printers use to broadcast their name on a network. The website afp548.com has some info on wide area bonjour

Scott

andrew, i got wake on demand to work wirelessly using file sharing, so i know it’s working.
but, exactly, how would i get web sharing to work? i read your svn example using lingon, but what would i enter as the terminal command for web sharing on port 80? would it be…
name: org.websharing.bonjour
what: dns-sd -R “HTTP” _http._tcp. . 80 pdl=application/httpserve

Andrew Bednarz

Scott, that should do the trick. The thing to remember is that you can run the command in terminal and use Bonjour Browser to check that its registered as you expect.

Scott

yes, that seems to work. the important thing is to restart the computer, not just log out, for it to take affect.
thanks for writing this article!

anthony

I really hope you can help me. I’ve been looking for information for a couple of days now and my head is starting to spin! Based on your insightful posts I;m hoping you could provide me with some answers.

I have a Macbook with Snow Leopard installed, Wake on Demand enabled and is connected wirelessly with a Livebox router (a UK model from Orange) i.e. not via an Airport Express /Base station.

1. In order to take advantage of Wake on Demand do I need to connect an Airport Express to my existing router?

2. Are there any applications similar to Wake On Lan (which only supports ethernet connections) that works in a wireless capacity? If so do i need and Airport Express?

3. Both of the above questions are in relation to my final question? I currently use Logmein but can’t access my Macbook from my works PC when the MB goes to sleep. Logmein says that it doesn’t offer Wake On Lan for wireless connected Macbooks. In light of the fact my MB can support wireless Wake on Demand, does this mean that in essence Logmein would need to install Bonjour at their end?

Andrew Bednarz

1. You need either an Airport Extreme or a Time machine, thats connected to your modem. I do not believe an Airport Express supports this as its not a router.

2. Wake On Wireless is supported by Airport extremes etc – but your Macbook’s wireless card also needs to support it. according to http://support.apple.com/kb/HT3774: Open System Profiler (located in /Applications/Utilities). In the Network section, select AirPort. If you see “Wake On Wireless: Supported”, your Mac supports Wake on Demand when connected to a wireless network and registered with the Sleep Proxy Server. If you do not see this entry, the Mac will only Wake on Demand when connected to your network with an Ethernet cable.

3. I don’t know exactly how Logmein works – but yes, they will need to register whatever ports they require with Bonjour. If you can find out what these are and how they work you could register them yourself with the method I described above. It may or may not be that easy – I have no idea what logmein does behind the scenes.

Sebastian

The only downside is that it of course only works if you have an Apple router.
I use the iPhone app “iNet Pro” for Wake on Lan/Demand. Works via the internet and the local network.

Andrew Bednarz

Yes, as I said in the 2nd paragraph, standard services such as file sharing and screen sharing already do Wake On Demand as long as you have a compatible base station and OS X 10.6 on the Mac.

David Klein

I guess I need more details. I have an Airport Extreme. I probably need to make changes on both it and my iMac.

David Klein

Let’s say I set it all up on the iMac and Airport Extreme.
I’m at work on my MBP. How do I start screen sharing and file sharing when I don’t have MobileMe/Back to my Mac?

Chad von Nau

I don’t know if this is true exactly. As far as Apple’s documentation http://support.apple.com/kb/HT3774 goes, Bonjour Sleep Proxy (Wake on Demand) doesn’t work over WAN without Back to My Mac. It’s not working for me anyway, so I’ve resorted to forwarding UDP port 9 to my machine and using the web wake-on-lan tool at http://rshut.com I have an Airport Extreme Base Station.

Chad von Nau

Uh.. scratch what I just said. It does work. I just was forwarding port 80 to my wireless IP instead of my wired IP.

Comments are closed.