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.

Mini-App of the Day: SmartSleep

SmartSleepSmartSleep is great for those who are constantly on the go, opening and closing their Macbook. SmartSleep gives the user great control over how and when the computer sleeps and hibernates.

Typically, you’ll probably want your Macbook to sleep when you close it (keep the contents of open files and apps that are running in RAM). Sleep is conveniently fast. If you’re a student like me, you don’t want to have to worry about corrupting your hardrive after throwing your laptop in a bag of some sort and running around campus or dropping it on the floor.

However, what if the batter charge is less than 10%? If you sleep your Macbook and the battery dies, you will lose anything stored in RAM that wasn’t previously saved to your hardrive. Thus, you’ll want to hibernate. But keeping track of the battery charge and changing this setting manually is cumbersome. This is where SmartSleep excels. You can set your computer to sleep until it reaches a certain charge level (e.g. 10%), at which point it will hibernate to protect your data in the event your battery dies.

Another nice thing about SmartSleep is it just sits in your preference pane; no extra menubar icons necessary.

If you don’t want to pay for the new version, I’d suggest downloading the older, free version from Softsonic.

SmartSleep Screenshor