Disable Sleep on a Mac from the Command Line with caffeinate
You’ve always been able to temporarily prevent a Mac from sleeping by using the pmset noidle command or a hot corner, but with modern versions of OS X, Apple has bundled a command line tool dedicated to sleep prevention much like the popular Caffeine app, and appropriately, named it caffeinate.
At it’s most simple usage, caffeinate just prevents sleep entirely, but you can modify the command with various flags to prevent just the display from sleeping, provide a specified time to avoid sleeping, prevent sleeping while a command runs, and more. A few useful examples are discussed below.
How to Disable Sleep with the ‘caffeinate’ Command in Mac OS X
To temporarily disable sleep functions regardless of sleep settings, run the following command from Terminal in Mac OS X:
At the core basic function of the command, caffeinate is all that’s necessary, and while caffeinate is active sleep will be prevented until it’s no longer running.
To stop caffeinate and return to normal sleep behavior, you can hit “Control+C” to quit out of caffeinate as it’s running like this, or you can kill it with the ‘killall caffeinate’ command if desired.
You can also run caffeinate for a pre-determined amount of time to prevent sleeping for a specified block of time, say for 4 hours while you download something, and then run it in the background by adding & to it:
caffeinate -t 144000 &
The number attached to the -t flag is the amount of time in seconds for sleep to be disabled on the Mac.
Caffeinate can also be attached to other commands to prevent the Mac from sleeping while that given command runs:
caffeinate [command_to_run] -arguments
That causes the Mac to avoid sleep only as long as it takes to run the command provided, after which the normal sleep rules apply.
With these latter tricks, if you run caffeinate in the background and want to cancel it then the easiest way to quit the process is to issue the kill command for the reference process ID, or simply ‘killall caffeinate’
To use the caffeinate command you’ll need to be running a fairly modern version of OS X, as the feature was introduced in 10.8 Mountain Lion and persists through 10.9 Mavericks, and into 10.10 Yosemite.
Heads up to MacWorld for the tip