Hibernate from Terminal

To hibernate from terminal you must use the pmset command. However, pmset only allows you to set the hibernate mode. It does not immediately put your machine to sleep. An example:

sudo pmset -a hibernatemode 25

There are 4 power management options:

  1. -b  —  Battery
  2. -c  —  Charger
  3. -u  —  UPS
  4. -a  —  All of the above

There are a total of 7 hibernation modes. The first 3 modes are standard. The last 4 modes may be used, though they are NOT recommended (as per the pmset man page):

  1. hibernatemode 0 — The system will not back memory up to persistent storage. The system must wake from the contents of memory; the system will lose context on power loss. This is, historically, plain old sleep. In binary, 0000 0000.
  2. hibernatemode 3 —  by default on supported portables. The system will store a copy of memory to persistent storage (the disk), and will power memory during sleep. The system will wake from memory, unless a power loss forces it to restore from disk image. In binary, 0000 0011.
  3. hibernatemode 25 — The system will store a copy of memory to persistent storage (the disk), and will remove power to memory. The system will restore from disk image. If you want “hibernation” – slower sleeps, slower wakes, and better battery life, you should use this setting. In binary, 0001 1001.
  4. hibernatemode 1 — Enables hibernation; causes OS X to write memory state to hibernation image at sleep time. On wake (without bit 1 set) OS X will resume from the hibernation image. Bit 0 set (without bit 1 set) causes OS X to write memory state and immediately hibernate at sleep time. In binary, 0000 0001 (bit 0).
  5. hibernatemode 2 — Causes OS X to maintain system state in memory and leave system power on until battery level drops below a near empty threshold (This enables quicker wakeup from memory while battery power is available). Upon nearly emptying the battery, OS X shuts off all system power and hibernates; on wake the system will resume from hibernation image, not from memory. In binary, 0000 0010 (bit 1 in conjunction with bit 0).
  6. hibernatemode 8 — Encourages the dynamic pager to page out inactive pages prior to hibernation, for a smaller memory footprint. In binary, 0000 1000 (bit 3).
  7. hibernatemode 16 — Encourages the dynamic pager to page out more aggressively prior to hibernation, for a smaller memory footprint. In binary, 0001 0000 (bit 4).

I’ve listed the binary because the command can be executed using the binary instead, which may be easier to remember for the last four options.

Option 5 is a very practical alternative and may be the way apps such as SmartSleep function (Read about SmartSleep in a previous post). However, when I set my SmartSleep setting to to “smart sleep” and then check my hibernate mode using pmset -g, the mode still reads as “0”. At any rate, the concept is similar, if not the same.

To make hibernate mode setting a bit easier to remember, I’ve added the following aliases to my .profile that I can execute on the command line:

alias hibernate='sudo pmset -a hibernatemode 25'
alias sleep='sudo pmset -a hibernatemode 0'
alias safesleep='sudo pmset -a hibernatemode 3'
alias smartsleep='sudo pmset -a hibernatemode 2'

Now I can easily how my system responds when I close my MacBook lid.

There is one final caveat: if you have an app like SmartSleep installed, it will override any hibernate mode settings you make. In testing this, I noticed how I would set hibernatemode 25 and SmartSleep to “smart sleep,” and then when I woke my machine up hibernatemode was set to “0”. Strange, but true.

8 Comments

  1. An alternative to closing the lid if you want to sleep your machine is the following: osascript -e ‘tell application “System Events” to sleep’

    Reply

    1. Or: `pmset sleepnow`.

      Reply

  2. or you can get an SSD =)

    Reply

    1. Too true. Though not all of us have $250+ we are willing to drop.

      Reply

  3. My favorite sleeping tweak is ‘sudo pmset lidwake 0’, so your MacBook doesn’t automatically awake from sleep when you open the lid.

    Reply

    1. A nice addition. Thanks.

      Reply

  4. Fantastic blog post … Thanks!!!

    Reply

  5. I’m reading and testing several times over and over but I still don’t understand what’s the difference between hibernatemode 1 and 25. Can somebody explain please?

    Reply

Comments, questions and feedback welcome.