The Frustration of the Sleep Command
MacOS is aggressively designed to conserve battery life. By default, if you step away from your MacBook, the screen will dim after a few minutes, and shortly after that, the entire system will go to sleep.
This is excellent for battery longevity, but it is a disaster if you are running a long background task. If you are rendering a massive 4K video in Final Cut Pro, compiling a huge Xcode project, or downloading a 100 GB database over a slow VPN, the Mac going to sleep will instantly pause or completely fail the operation.
To prevent this, many users download third-party menu bar apps like Amphetamine. However, you don’t need to clutter your Mac with third-party software. Apple has built a native, incredibly powerful command-line tool directly into macOS specifically for this purpose: caffeinate.
Step 1: The Basic Caffeinate Command
Open the Terminal application (found in Applications > Utilities).
To instantly prevent your Mac from going to sleep, simply type the command by itself and press Enter:
caffeinate
When you run this, it will appear as though nothing has happened; the terminal will just sit there with a blinking cursor. However, as long as that terminal window remains open and that command is running, your Mac will never go to sleep. It will stay fully awake indefinitely.
When your massive download finishes three hours later, click on the terminal window and press Ctrl + C to terminate the command and return your Mac to its normal power-saving schedule.
Step 2: Caffeinating for a Specific Duration
Leaving caffeinate running indefinitely can be risky if you forget about it, as it will drain your battery over night.
If you know a task (like a Time Machine backup) usually takes about two hours, you can tell caffeinate to keep the Mac awake for exactly that amount of time, using the -t (timeout) flag, followed by the duration in seconds.
For a two-hour timer (2 hours * 60 minutes * 60 seconds = 7200 seconds):
caffeinate -t 7200
The command will run silently. Exactly 7,200 seconds later, the command will automatically terminate itself, allowing the Mac to safely go to sleep.
Step 3: Tying Caffeinate to a Specific Program
The most powerful way to use caffeinate is to bind it to a specific application or process. Instead of guessing how long a download will take, you can tell macOS: “Keep this computer awake until this specific program finishes.”
You do this using the -i (idle) flag, followed by the command you want to run.
For example, if you are using the terminal to download a massive file using curl, you can wrap the command like this:
caffeinate -i curl -O https://example.com/massive_database.iso
The Mac will stay wide awake for the entire duration of the download. The absolute millisecond the curl command finishes, the caffeinate command releases its hold, and the Mac is allowed to go to sleep. This is perfect for overnight scripting.
Step 4: Keeping the Display Awake
By default, caffeinate prevents the system from sleeping (so background tasks continue), but it still allows the display to turn off to save power.
If you are giving a presentation, running a dashboard on a monitor, or reading a long recipe and you explicitly need the screen to stay lit up, you must use the -d (display) flag.
caffeinate -d -t 3600
This command will force the screen to remain at full brightness for exactly one hour, completely ignoring the energy saver settings in System Preferences.