Hibernation on newer MacBook Pros

Apparently, since Apple started shipping MacBook Pros (MBPs), the original way of sleeping used on the Powerbook wasn’t ported over to the MBP. On Powerbooks, if you would let it go to sleep, it would just keep RAM powered on, sleep almost instantly, and wake rapidly. An MBP also keeps RAM powered on by default, but also writes its memory contents to a sleep image. The machine still wakes rapidly, but sleeping takes a lot more time. In my case, it needs to write 4 gigabytes of RAM to that sleep image (which resides in /var/vm/ by the way). I don’t like putting my MBP in my bag while I don’t have the audible confirmation of the fans that it’s turned off, so I decided to write a bash script with some information about different sleep modes in the back of my head.

The information that I found was that there are basically three (or five) different sleep modes:

  1. Fast sleep mode — RAM is still powered on, and nothing is written to the hard drive. Sleeping is fast, and waking is fast. Advantage: it’s fast!
  2. Hibernation — RAM contents are written to disk and the MBP just powers down. Sleeping takes some time, and waking takes some time. Advantage: no additional power is needed to stay in hibernation mode.
  3. Safe sleep mode — RAM contents are written to disk, and RAM is still powered on. Sleeping takes some time, and waking is fast. Advantage: if the MBP runs out of battery while in safe sleep mode, hibernation mode is entered automatically, so no data will be lost.
  4. Same as 1, but for MBPs that use secure virtual memory.
  5. Same as 3, but for MBPs that use secure virtual memory.

In OS X, you can manipulate power management settings using pmset. To see what mode your MBP is currently in, issue the following command in a terminal:

pmset -g | grep hibernatemode

You’ll probably see that your MBP is in hibernatemode 3. To change the hibernatemode, you can issue the following command:

sudo pmset -a hibernatemode n

where n is the number of the desired sleep mode in the list above.

To make life a bit easier, I wrote a script to make these changes on the command line. You can download the script, or just copy/paste the lines that I’ve pasted below into a file on your harddrive. Call it hibernate and put it in ~/bin/ and make sure that ~/bin/ is in your PATH environment variable. Don’t forget to make it executable as well! I assume that you know how to do all that. If not, please leave a comment. 😉

The script takes three parameters:

  • -f — Enables fast sleep mode.
  • -h — Enables hibernate mode.
  • -s — Enabled safe sleep mode, which is default on newer MacBook Pros.
Without parameters, it prints usage information and the current sleep mode. Here comes the script.
#!/bin/bash
case "$1" in
	-f) 	sudo pmset -a hibernatemode 0
		result=$?
		if [ $result -eq 0 ];then
			sudo rm /var/vm/sleepimage
			echo "Fast sleep enabled and sleep image removed"
			exit 0
		else
			echo "Password incorrect or not entered?"
			exit 1
		fi
		;;
	-h)	sudo pmset -a hibernatemode 1
		result=$?
		if [ $result -eq 0 ];then
			echo "Hibernate enabled"
			exit 0
		else
			echo "Password incorrect or not entered?"
			exit 1
		fi
		;;
	-s)	sudo pmset -a hibernatemode 3
		result=$?
		if [ $result -eq 0 ];then
			echo "Safe sleep enabled"
			exit 0
		else
			echo "Password incorrect or not entered?"
			exit 1
		fi
		;;
	*)	echo "Hibernate script by Franklin van Velthuizen"
		echo "usage: hibernate [-fhs]"
		echo "-f	Enable fast sleep mode"
		echo "-h	Enable hibernation mode"
		echo "-s	Enable safe sleep mode (default on newer MacBook Pros)"
		echo ""
		currentmode=`pmset -g | grep hibernatemode`
		set -- junk $currentmode
		shift
		if [ -n "${2+x}" ]; then
			if [ $2 -eq 0 ]; then
				echo "Current mode: Fast sleep mode"
			else if [ $2 -eq 1 ]; then
				echo "Current mode: Hibernation mode"
			else if [ $2 -eq 3 ]; then
				echo "Current mode: Safe sleep mode"
			else
				echo "Current mode: unknown"
			fi fi fi
		else
			echo "Current mode: unknown"
		fi
		exit 1
		;;
esac

Leave a Reply

Your email address will not be published. Required fields are marked *