The Annoyance of Sleep Mode During Long Tasks
macOS is highly optimized to save battery life and reduce thermal load by putting the computer to sleep after a period of inactivity. Usually, this is desirable. However, if you are downloading a massive file, rendering a complex 4K video overnight, or running a lengthy terminal script, your Mac going to sleep will instantly interrupt and pause the process.
While you could open System Settings, navigate to the Displays or Energy Saver menu, and change your sleep timeout to “Never,” remembering to change it back later is tedious. Many users rely on third-party apps like Amphetamine or Caffeine for this, but macOS has a powerful, native Terminal command built right in called caffeinate.
How to Use the caffeinate Command
The caffeinate command temporarily creates assertions in the macOS power management system, preventing the system from idling into sleep mode.
Method 1: Prevent Sleep Indefinitely
To keep your Mac awake indefinitely, open the Terminal app (located in Applications > Utilities) and simply type:
caffeinate
Press Enter. The command prompt will disappear, indicating the command is running. Your Mac will not go to sleep as long as this terminal window remains open and the command is active.
To stop it and allow your Mac to sleep normally, press Ctrl + C in the Terminal window.
Method 2: Prevent Sleep for a Specific Amount of Time
If you know your download will take about two hours, you can use the -t flag to specify a duration in seconds. After the timer expires, the command terminates automatically.
To keep the Mac awake for exactly 2 hours (7200 seconds):
caffeinate -t 7200
Method 3: Tie caffeinate to a Specific Process
This is the most powerful use case for developers and power users. Instead of a timer, you can tell caffeinate to keep the Mac awake only as long as a specific command takes to run.
Suppose you have a script called backup_data.sh that takes several hours. You can prepend caffeinate to the command:
caffeinate ./backup_data.sh
The Mac will stay awake for however long the backup script runs. The exact millisecond the script finishes, caffeinate shuts down, and macOS can go to sleep.
Advanced Flags for Specific Hardware
By default, caffeinate prevents the system from sleeping, but it still allows the display to turn off. Depending on your needs, you can modify this behavior with specific flags:
-d(Display): Prevents the display from turning off. Useful if you are monitoring a dashboard.-m(Disk): Prevents the hard disk from spinning down.-s(System): Prevents the system from sleeping entirely when plugged into AC power.
To combine flags, simply string them together. To prevent both the system and the display from sleeping for one hour (3600 seconds):
caffeinate -dt 3600
Conclusion
The caffeinate command is a perfect example of macOS’s robust Unix underpinnings. By using this built-in utility, you can ensure critical long-running tasks finish uninterrupted without the need to install third-party menu bar apps or fiddle with your system-wide Energy Saver preferences.