Friday, October 13, 2006

SafeSleep (a.k.a. Hibernation) for Intel Macs

Don't you just love how easy it is to put your Mac to sleep when you close the lid on your notebook? And how instantly your Mac can wake up when you open the lid later on? It's just great and it is very reliable.

But while the computer is in the "sleep" mode it still consumes some energy. Usually it's nothing worth considering, but when you are traveling for couple of days you might want to save every bit of battery energy possible.

Both Windows and Linux offer you an option of hibernation. Of course MacOS supports hibernation as well but Apple refers to this feature as "SafeSleep". While experimenting with my Macbook Pro I've noticed that SafeSleep is used every time the battery is totally drained and there is not enough energy for powering the Sleep mode. When you plug in the laptop, ahem notebook, to the power adapter, it will use the hibernation file to recover the content of the RAM and thus the OS state. In fact the hibernation file is created every time the computer goes to Sleep mode so taking out the battery while the computer is in the Sleep mode will have the same effect.

I was pointed to this article that discusses the pmset command line tool and describes how to use it to prevent Macs notebooks from waking up on opening the lid or setting up the power button to put the computer to sleep without displaying the "shutdown menu".

pmset tool can be used to set the hibernation/sleep mode:
pmset -a hibernatemode 0 # Sleep mode, but don't create a hibernation file
pmset -a hibernatemode 1 # SafeSleep (Hibernation) mode
pmset -a hibernatemode 3 # Sleep mode + create hibernation file (Default)
When putting MacOS to sleep it will do whatever hibernatemode is set to.

I also found this article that describes how to create an apple script that will enable you to hibernate the computer but launching it (without having to fiddle around with terminal). But unfortunately this script doesn't work for me so I fixed it a little:

do shell script "/usr/bin/sudo -k;/usr/bin/sudo /usr/bin/pmset -a hibernatemode 1; /usr/bin/sudo -k" with administrator privileges
ignoring application responses
tell application "Finder" to sleep
do shell script "(/bin/sleep 25 && /usr/bin/sudo -k && /usr/bin/sudo /usr/bin/pmset -a hibernatemode 3 && /usr/bin/sudo -k) &> /dev/null &" with administrator privileges
end ignoring
Store this 5-line script via script editor as "Application Bundle" with "Run Only" option checked and you are good to go. I've noticed that when you save it as "Application" it still doesn't work.

One last note for PowerPC MacHeads: Some Powerbooks support SafeSleep as well, but it involves a little more work. Check out this article for more details.

No comments: