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 OS X Mountain Lion, 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.
To temporarily disable sleep, run this command from Terminal:
You can also run caffeinate for a pre-determined amount of time to prevent sleeping, 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 the 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.
Heads up to MacWorld for the tip