How to keep your Mac awake: settings, caffeinate and the lid-closed rules
A Mac goes to sleep to save power, which is exactly what you want most of the time. It’s a problem when something needs to finish while you step away: a big download, a backup, a build, a render, or an AI agent working through a long task.
Here are the ways to keep a Mac awake on macOS 27, from a settings switch to a one-word Terminal command, plus the rules for keeping it awake with the lid closed.
Option 1: System Settings
On a MacBook:
- Open Apple menu > System Settings > Battery.
- Click Options.
- Turn on Prevent automatic sleeping on power adapter when the display is off.
On a desktop Mac (iMac, Mac mini, Mac Studio): open System Settings > Energy and turn on Prevent automatic sleeping when the display is off.
To control when the screen itself turns off, go to System Settings > Lock Screen and set Turn display off on power adapter when inactive (and on battery on a laptop).
The catch: these are permanent settings. Most of the time you only need the Mac awake for one job, which is where caffeinate is better.
Option 2: caffeinate, built into every Mac
caffeinate keeps the Mac awake while it runs and lets it sleep normally again when it stops. Open Terminal and type:
caffeinate -i
Your Mac won’t idle-sleep until you press Control-C. The useful flags:
| Flag | What it prevents |
|---|---|
-i |
Idle sleep of the system (the one you usually want) |
-d |
The display from sleeping |
-s |
System sleep, but only while on power |
-m |
The disk from idle sleeping |
-t 3600 |
Sets a timeout in seconds (here one hour) |
-w <pid> |
Stays awake until the process with that ID exits |
The best trick is to put caffeinate in front of the command you’re running. The Mac then stays awake exactly as long as that command and no longer:
caffeinate -i ./long-build.sh
Keeping your Mac awake for Claude, Codex and other agents
AI coding agents can run for a long time without you touching the keyboard, which looks like “idle” to macOS. Two ways to keep them going:
- Start the agent’s command with
caffeinate -iin front of it, as above. - Or, if the agent is already running, keep the Mac awake until it finishes: find its process ID and run
caffeinate -i -w <pid>.
Some desktop apps already do this for you. On our test Mac, pmset -g assertions listed the Claude desktop app holding a “NoIdleSleepAssertion” while it worked.
See what’s keeping your Mac awake
If your Mac won’t sleep when you want it to, or you want to confirm your trick worked, run:
pmset -g assertions
Each line starting with pid names a process that’s holding the Mac awake and why. pmset -g on its own shows the current sleep settings, and the sleep line notes which processes are preventing sleep.
Keeping a Mac awake with the lid closed
Closing a MacBook’s lid puts it to sleep, and caffeinate doesn’t change that. Apple’s supported way to use a closed MacBook is closed-display mode, which needs:
- a power adapter (or a display that charges the Mac over USB-C or Thunderbolt),
- an external display,
- an external keyboard and mouse or trackpad.
Connect everything, wait for the desktop to appear on the external display, then close the lid. Without an external display, the Mac sleeps when the lid closes; that’s by design.
Apps that do this from the menu bar
If you’d rather click than type, menu bar apps such as Amphetamine (free on the Mac App Store) wrap the same mechanism in a menu, for example keeping the Mac awake for a set time or while a certain app is running.
Related
- Running long jobs often fills the disk. Keep an eye on it with free disk space in your menu bar.
- Too many menu bar apps already? Here’s how to hide menu bar icons.
Questions
How do I keep my Mac awake with the lid closed?
Apple supports closed-display mode: the Mac must be connected to power, an external display, and an external keyboard and mouse or trackpad. Without an external display, a MacBook goes to sleep when you close the lid.
What is the caffeinate command?
caffeinate is a built-in Terminal command that stops your Mac from sleeping while it runs. caffeinate -i blocks idle sleep, -d keeps the display on, and -s blocks system sleep while on power. Press Control-C to stop it.
How do I keep my Mac awake while Claude or another AI agent works?
Run the agent with caffeinate in front of it, for example caffeinate -i followed by the command, so the Mac stays awake exactly as long as the agent runs. Some desktop apps, including Claude's, also hold their own keep-awake assertion while working; pmset -g assertions shows which ones do.