Want CNET to notify you of price drops and the latest stories?

Caffeinate Mountain Lion to prevent it from sleeping

The "caffeinate" Terminal command offers some quick options for keeping sleep from interrupting your Mac.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
4 min read

While setting your Mac to automatically sleep after a period of inactivity has its energy-saving benefits, you may find that the system will go to sleep even when it is churning through data (e.g., applying a lengthy filter routine to groups of images).

This issue happens because Apple has implemented some changes to the latest versions of OS X that result in different requirements for the system to determine if it can go to sleep, and one of these is a new dependence on applications to invoke a "power assertion" to keep the system from going to sleep.

OS X Sleep Settings
While you can adjust the OS X sleep settings to prevent it from sleeping, this may not be the most desired approach. Screenshot by Topher Kessler/CNET

Unless a program invokes a power assertion, the system may go to sleep if left idling, even though the program is running. This usually is not much of a problem, but sleep will pause all activity and may cause an error in the program, especially if the routine being run needs to be continuous.

If you find that your Mac goes to sleep even when specific applications are active, there are a couple of things you can do about it.

Increase sleep delay
The first approach is to change the sleep patterns of your Mac by going to the Energy Saver system preferences and adjusting the sliders to have the system sleep at different times (keep in mind different slider settings will be used for different power sources). However, this merely avoids the situation and does not directly address it.

Caffeinate your Mac
Caffeinating the system basically means to manually create a power assertion in the system, which can be done globally or targeted to a specific application so the system will not sleep while that application is running.

While there are third-party utilities like Caffeine, or Sleep No More, Apple has a couple of ways you can do this directly in OS X, so these tools are not always needed.

The first approach is to set a global power assertion on the system, which can be done by opening the Terminal and entering the command "pmset noidle." When you do this the terminal will not return to the command line until you press Control-C to cancel, or close the Terminal window and kill the running process.

In Mountain Lion, Apple has introduced a new command called "caffeinate," which if run directly will perform the same behavior as the previous "pmset noidle" command. While similar to "pmset noidle," the caffeinate command has a couple of additional options that make it far more useful. These options are described in detail in the "caffeinate" manual page, but their use is rather simple and straightforward.

The first feature is support for timeouts, so if you only want the assertion to be on for a short period of time, then you can use the "-t" flag followed by the number of seconds to keep the assertion on, such as the following for a 5-minute session in which the system will not sleep:

caffeinate -t 300

Additional flags can be used to prevent idle sleep (-i), prevent display sleep (-d), prevent sleep when on AC power (-s), and another option to keep the current user account in an "active" state (-u). Therefore, to use these options to customize how the system will (or more accurately will not) sleep, then you can enter combinations of them such as the following:

caffeinate -dsi -t 600

A last feature of the caffeinate command is its ability to target a specific program and prevent the system from going to sleep while that program is open. OS X applications are in fact folder "bundles" that hold the program itself along with some resources the system and the program use. An alternative to opening applications by double-clicking them is to open their executable in the Terminal, and with this approach you can precede the opening command with "caffeinate" to only target that specific program. For instance, the following command in the Terminal will open TextEdit:


If you would like to caffeinate this program, then you can run the following to ensure the system does not sleep while the program is open:

caffeinate /Applications/TextEdit.app/Contents/MacOS/TextEdit

caffeinate command in OS X
The command will not return to the command prompt until you quit TextEdit or whatever program you have caffeinated. Screenshot by Topher Kessler/CNET

The caffeinate command's various flags can also be used to customize items like display sleep and idle sleep as well. In the following example, TextEdit will launch but display and idle sleep will only be prevented in the first 10 minutes following its launch.

caffeinate -di -t 600 /Applications/TextEdit.app/Contents/MacOS/TextEdit

While the caffeinate command can be used in this method to target programs you use in OS X, its main benefit comes for those who use scripting heavily in their systems for backups and other purposes. Sometimes scripts you create can take hours to complete, so to prevent the system from sleeping in the middle of them you can invoke them with caffeinate, or include this command in the script to prevent sleeping during its execution.

Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.