How to prevent sleep in OS X
There are several options available in OS X to keep the system from going to sleep in the middle of tasks.
System sleep in OS X systems is convenient to have for saving power without interrupting your workflow, especially for laptop systems. This is easy enough to set up in the OS X system preferences; however, there may be times when despite having sleep enabled you might have an important task running and wish to prevent the system from going to sleep.
In past versions of OS X, the system would not enter sleep if applications were running and writing files to disk or running lengthy computational tasks, but this has changed in the latest releases of OS X, wherein order to keep the system from going to sleep. If a program does not do this, then the system may halt its activity and go into sleep mode regardless of what the program is doing. Luckily user activity and other features keep the system awake so this is rarely a problem, but it may affect your system if you have a custom script or program running that does not invoke an assertion.
If you would like to prevent a Mac system from going to sleep, you can do so in several ways:
- Turn off sleep
The first option is to disable system sleep entirely, which can be done in the Energy Saver system preferences, or by running the following command in the Terminal (in the /Applications/Utilities/ folder):
sudo pmset sleep 0
While this is a fairly straightforward and obvious approach, it is easy to forget this change and leave the system always running, or you might forget specific time settings for when the system is to put itself to sleep.
- Prevent idle system sleep
Since the system goes to sleep when it has been idle for a set period of time provided there are no power assertions set, you can manually invoke a power assertion to prevent this from happening. To do this, open the Terminal utility and run the following command:
This command will invoke the PreventUserIdleSystemSleep power assertion and keep the power manager from sleeping the system. The assertion will remain active until you press Control-C to stop the command or close the Terminal window running the command.
- The caffeinate command
In Mountain Lion, Apple has included a new command called "caffeinate" that if run by itself performs a similar action as the "pmset noidle" command mentioned above, where it will invoke a power assertion to prevent the system from going to sleep and keep it active until the command is cancelled. The difference between these two commands is that caffeinate is meant to be used in conjunction with scripts and other utilities that may not have power assertion features and be susceptible to interruption from system sleep.
To use the caffeinate command, if you have a utility or script you intend to run and wish to prevent any interruption from the system, then use caffeinate before it, similar to how you would run "sudo" to run a command in administrator mode:
When run in this manner, the script or utility will execute normally, but a power assertion will be set that will not allow the system to go to sleep. When the script finishes running, the caffeinate command will also exit to allow proper system sleep if needed. Using caffeinate in this manner may be convenient to use for custom backup scripts and similar important routines you may have on your Mac systems.