Saturday, 18 September 2010

Wake-on-LAN workaround for Mac Snow Leopard

This article discusses Wake-on-Demand versus "classic" Wake-on-LAN on Mac Snow Leopard.

Apple's Snow Leopard operating system introduced Wake On Demand (WoD).  This is an open standard, intended to be a more user-friendly version of Wake On LAN (WoL) which has been around for years.

Wake On Demand allows computers to save energy by sleeping when idle, whilst waking up automatically if another machine on your network requests service.  So for example, your iMac can save energy by sleeping, but it will wake up quickly if you need to copy a file over to your MacBook; access an iTunes library; or log in remotely via Remote Desktop or SSH.  The Wake On Demand feature is enabled by ticking the box marked Wake for network access under System Preferences / Energy Saver.  Sensibly, laptops don't wake up with lid closed or without mains power.

Whilst the Wake On Demand feature is well-intentioned, it has an unwelcome side-effect: the sleeping computers wake up every two hours (day and night) to check the local network configuration and refresh the Bonjour proxy cache.  I don't like this: it's a waste of energy (the computer won't sleep again until the usual timer expires); it unnecessarily stresses the machine; and it's annoying if someone's sleeping in the same room as the computer.

The Wake On Demand feature depends on a device on your network running a Bonjour Proxy Cache server.    Recent firmware for the Apple Time Capsule and Apple Airport Express wireless access points implement a Bonjour Proxy service.  I've already blogged about one side-effect of this here: Apple Time Capsule steals IP addresses, but that's OK really.   An always-on Mac can also run the Bonjour Proxy service (e.g. if Internet connection sharing is enabled).

Wake on LAN is an open standard that predates Wake-on-Demand.   To wake up a computer using WoL, you just need to send a "magic WoL packet" to the computer's Ethernet MAC address.

Annoyingly, the Snow Leopard OS doesn't offer any way to enable WoL without enabling WoD. However, there is a workaround documented in an article at the link below:-
From the article:-
"A comment in mDNSResponder's open source code offers some explanation:
we still want to wake up in at most 120 minutes, to see if the network environment has changed. E.g. we might wake up and find no wireless network because the base station got rebooted just at that moment, and if that happens we don't want to just give up and go back to sleep and never try again.
...So how do I turn off this auto-wake feature then?
The code in mDNSResponder makes only two checks on when not to schedule the maintenance wakes:
  • If 'Wake for network access' is turned off
  • if there are no Bonjour-advertised services on your system
This check is always made at the point the machine is about to go to sleep. After much experimentation, I finally have a reliable way of defeating this check without really doing either of the above.

Using sleepwatcher, add the following commands to your script to run when your computer is going to sleep:
    /bin/sleep 1
    /usr/sbin/systemsetup -setwakeonnetworkaccess on >/dev/null
And add this to your 'wakeup' script:
    /usr/sbin/systemsetup -setwakeonnetworkaccess off >/dev/null
Finally, also execute that last command right now, or just manually turn 'Wake for network access' off in the Energy Saver preferences.
What this does is turn wake for network access on only at the very last moment when your machine is going to sleep. This way, mDNSResponder will be fooled into thinking you don't have network wakes enabled, so it won't schedule the unwanted maintenance wake. In reality, though, your sleep script will enable it anyway, but in such a way that mDNSResponder doesn't see it."
As an alternative to Sleepwatcher, you might try disabling Bonjour advertisements [Apple.com] if you don't mind not being able to resolve the hostnames of any machines on your local network that don't have DNS entries.  (Non-geeks: don't try this at home unless you have your own DNS nameserver, or you only have one computer on your network at any time.)

If you use the Ports (formerly Fink) framework, you can install Sleepwatcher using one command:-
        sudo port install sleepwatcher
Then just follow the instructions.  I placed the commands from the Macworld article into the files /opt/local/etc/rc.sleep and /opt/local/etc/rc.wakeup.  But by default those scripts allow for per-user sleep and wakeup commands.

You might also find these links useful:-
The last of these links states: "Macs that have Wake on Demand enabled will occasionally wake for a brief time, without lighting the screen, in order to maintain registrations with the Bonjour Sleep Proxy. On some Macs, sounds from the optical drive, hard drive, or fans may be heard during these brief maintenance wakes."  (I don't believe these maintenance wakes are brief: they appear to use the normal timeouts to determine when the next sleep occurs.)

Sending the magic Wake-On-LAN packet
    If you're not relying on a Bonjour proxy server, how can you send the magic WoL packet to wake up your computer?  Well, you could write a trivial piece of code to spit out the single Ethernet frame that's needed, or you could download such a program from the Internet.   But if you use Monowall or pfSense as your VPN firewall, then you'll find the firewall's GUI already offers a web page (Services / Wake on LAN) that lets you store a list of MAC addresses for your network, so that you just click on an entry to wake up that computer - see screenshot below:-


    You can probably wake up every box on a LAN segment by sending the magic WoL packet to the broadcast MAC address (all FF's) but I've never tried that.

    7 comments:

    1. PS... If the Mac won't go to sleep (or stay asleep), do you have a Canon USB scanner plugged in? I've got a Canon LiDE 500F scanner, bought from the Apple web store, but the drivers are awful and they stop the Mac from sleeping. Unplugging the scanner and rebooting solves the problem. It's a pity Apple didn't test the scanner before selling it in their web store. Recommendations gratefully received for an affordable USB scanner with really decent OS X support.

      ReplyDelete
    2. Most USB and FireWire scanners are supported by Vuescan, an awsome Mac scanning application. I would go with HP or Epson.

      ReplyDelete
    3. Does the Mac support Wake on LAN from a shutdown or hibernated machine? I can only get mine to work from sleep.

      ReplyDelete
    4. Hi Ryan. No, not as far as I can tell. There's no setting for that in the GUI, nor is there any mention in "man pmset".

      ReplyDelete
    5. can I ask you this - you say you use Airport express - i do to, but not as my main router, just as a client. i get intermittent successes trying to use the WoD feature (and I don't want to use AEX as my router as i have a great one).

      what do I need to open in my router (ports etc) to make it work, currently it doesn't work (macs do not wake up)?

      ReplyDelete
      Replies
      1. Hi. Thought you were talking about Wake On LAN :-
        http://en.wikipedia.org/wiki/Wake-on-LAN
        (- No specific port, it actually triggers based on the content inside an ethernet frame addressed to your target MAC)

        but perhaps you're talking about Wake On Demand (Apple) :-
        http://support.apple.com/kb/ht3774
        (- That one's all about Bonjour Sleep Proxies)

        Either way, you should be able to wake up your Macs if they connect via Ethernet (and the right settings are set in Preferences). But Wakeup over Wireless networks only works in some very recent Macs, and I've not used it myself.

        Hope this helps

        Delete
      2. Just reading your question again. If WoD isn't working locally, then you need something that's capable of acting as a bonjour sleep proxy. So you'd need an Apple device of some sort, unless other manufacturer's Bonjour-aware devices now provide this service. It's not simply a question of opening up a port on a router - unless you're talking about sending a WoL packet from a remote network.

        Delete

    Spammers: please stop wasting my time. All comments are moderated before publication.