Disable Sleep with Caffeinate in MacOS

How to disable sleep with caffeinate in MacOS.

September 5, 2023


Caffeinate has been bundled with MacOS since 10.8 Mountain Lion. This utility is particularly useful when you need to prevent your Mac from going to sleep during long-running tasks.

To temporarily disable sleep, run the following command:

caffeinate

While caffeinate is active, sleep is prevented.

caffeinate

Prevent the system from sleeping on behalf of a utility.

Syntax
    caffeinate [-disu] [-t timeout] [-w pid] [utility arguments]

- `-d`: Prevent the display from sleeping.
- `-i`: Prevent the system from idle sleeping.
- `-s`: Prevent the system from sleeping. This setting is only active when the machine is running on AC power.
- `-u`: Declare that user is active. If the display is off, this option will turn the display on and prevent the display from going to sleep.


Location: /usr/bin/caffeinate

To stop caffeinate:

killall caffeinate

Or press "Control+C".

Prevent sleeping for a specified block of time

Add flags to prevent sleeping for a specified block of time:

caffeinate -t 7200 &

The number attached to the -t flag is the amount of time in seconds (7200 seconds = 2 hours). Adding & runs the process in the background, allowing you to continue using the terminal for other tasks.

Prevent sleeping while a command is run

Caffeinate can also be attached to other commands to prevent the Mac from sleeping while that given command runs:

caffeinate [command_to_run] -arguments

This command will avoid sleep only as long as it takes to run the command provided, after which normal sleep rules apply.


Originally published at blog.jdwilson.ca on September 17, 2016.