21 Comments

Summary:

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 […]

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?

  1. What are some other use cases for this?

    Share
    1. For example, can I wake up my computer remotely to get a file from it?

      Share
  2. 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.

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

      Share
    2. 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?

      Share
    3. If you don’t have Back To My Mac, you need to poke appropriate holes in your firewall, port forwards etc. See http://theappleblog.com/2009/06/22/how-to-remotely-control-your-mac-over-the-internet/ If the forward is set up correctly, if you have a compatible airport and 10.6 the wake on demand will just work.

      Share
      1. 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.

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

        Share
  3. [...] Wake On Demand with only a little bit of work to set it up. Here’s how I did it. Read more at The Apple Blog AKPC_IDS += "508,"; Share this Post: Tagged [...]

    Share
  4. 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.

    Share
  5. 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?

    Share
    1. 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.

      Share
  6. 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

    Share
    1. 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.

      Share
    2. 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!

      Share
  7. 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

    Share
  8. [...] DynDNS does not get updated until the computer wakes up. When wanting to use Snow Leopard’s Wake On Demand feature this is a problem, so this has meant “tough luck,” requiring you to run the DynDNS Mac [...]

    Share
  9. [...] DynDNS does not get updated until the computer wakes up. When wanting to use Snow Leopard’s Wake On Demand feature this is a problem, so this has meant “tough luck,” requiring you to run the DynDNS Mac [...]

    Share
  10. [...] DynDNS does not get updated until the computer wakes up. When wanting to use Snow Leopard’s Wake On Demand feature this is a problem, so this has meant “tough luck,” requiring you to run the DynDNS Mac [...]

    Share

Comments have been disabled for this post