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:
- -b — Battery
- -c — Charger
- -u — UPS
- -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):
- 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.
- 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.
- 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.
- 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).
- 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).
- 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).
- 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.