Troubleshooting sleep in OS X
Several settings and system features govern the sleep mode in OS X and can prevent normal system sleep if either in use or used incorrectly.
System sleep in OS X is a convenient feature that saves power while preserving your work flow. This option has been available in Macs and other PC systems for years, and is especially useful for laptops where you might wish to conserve battery power. While computer sleep is nothing new, there are some details of how it is implemented in OS X that may help you troubleshoot situations where sleep does not behave as it should.
For Mac systems sleep is governed by the system management controller (SMC), which has a number of variables it uses to manage when disks spin down, what GPU to use, whether or not various sensors are enabled, and options for managing sleep behaviors. Some of these sleep behaviors include when the display and system go into sleep modes (these are set in the System Preferences), whether or not the system will use hibernate mode (enabled by default on laptops) and where it will store the hibernation file on the system.
To see these variables, open the Terminal and run the following command:
By default, the hibernatemode variable should be 0 for desktops and 3 for laptops, the difference being the mode for laptops enables the use of the hibernation file to store RAM contents and completely shut down the system, as a backup to normal sleep mode. A third potentially useful option here is the value of 25, which will have the system only use the hibernate file and thereby conserve more energy when asleep, with the drawback being longer sleep and wake times since the RAM contents need to be purged during sleep.
The sleep mode variable can be set to either 0, 3, or 25 by running the following command (be sure to only use one of these three values to avoid potential errors in the sleep process):
sudo pmset hibernatemode NUMBER
If your system is set to use hibernate mode and store the contents of RAM, then the SMC will also use a hibernation file, which is located in the folder designated by the hibernatefile variable for the SMC (usually the hidden /var/vm/ folder). To see this file, in the Finder choose "Go to Folder" from the Go menu, and then enter /var/vm/ in the field. When executed you will see a file called "sleepimage" that is the same size as the RAM in your computer (in other words, 4GB or 8GB if you have that amount of RAM installed).
If for some reason your system is not going into hibernation mode, or shuts down and reboots instead of restoring to a working state after going into hibernation, then you might benefit from deleting the sleepimage file in the /var/vm/ folder and allowing the system to recreate it.
Another aspect of the system management controller that might affect system sleep and that's relatively new to OS X is the power assertions feature. This is an option in which a program or process can invoke an assertion to keep the system awake regardless of the SMC's settings in order to finish a task. To see these assertions, open the Terminal and run the following command:
pmset -g assertions
The output from this command will list the assertion types along with the number of each assertion that is invoked in the system. After this the output will list both processes that have invoked the assertions and any kernel-based assertions that might be active. If you have a Mac that will not go to sleep regardless of what settings you put in the Energy Saver system preferences, then it may be that a program is maintaining a power assertion that prevents it from doing so.
Troubleshooting power assertions is relatively simple, especially since any listed by the output of the pmset command above will include the name of the process along with its process ID (PID). These details can help you track down the program and determine how best to manage it. If it is a relatively benign background process such as the help viewer (helpd) or a hard-drive indexing program (mds), then you can use Activity Monitor to look up the process by name and PID, and then quit it. If you are unsure what the listed processes do, then be sure to look them up beforehand.
Another issue with Mountain Lion's use of power assertions is that these are used instead of more global features like disk activity when determining whether or not the system can go to sleep, and therefore the system may go to sleep mode in the middle of some tasks like importing pictures or otherwise transferring data if the developer has not programmed it to hold a power assertion when performing these tasks.
If you are using a program for which the system ignores its activity and goes to sleep, then you can either extend or disable the system's sleep timer, or you can use a program like Caffeine to keep an assertion active and thereby keep the system awake.