Keep your Mac awake for Claude Code, agents and long jobs.

A Mac that goes to sleep pauses your agent, your download or your build. The quickest fix comes with macOS: one Terminal command, caffeinate. Here are the ways that work, and the trap when you close the lid.

Updated

The short version

  • caffeinate -i keeps the Mac awake until you press Control-C. Put a command after it and the Mac stays awake exactly as long as that command runs: caffeinate -i claude.
  • No Terminal: on a MacBook, turn on Prevent automatic sleeping on power adapter when the display is off (Battery → Options). On a desktop Mac, the same switch is under Energy.
  • Closing a MacBook's lid puts it to sleep, unless it's on power with an external display, keyboard and mouse.
  • The screen can lock and turn off while the Mac keeps working. Sleep is what stops your jobs, not a dark screen.

1caffeinate: the built-in command

caffeinate ships with every Mac. It keeps the Mac awake while it runs and does nothing else.

Keep the Mac awake until you press Control-C:

caffeinate -i

Keep it awake exactly as long as Claude Code (or any other command) runs:

caffeinate -i claude

For two hours (7,200 seconds):

caffeinate -i -t 7200

Until a program that's already running finishes (find its number with pgrep, then use it after -w):

pgrep -fl claude
caffeinate -i -w 12345

What the letters mean: -i stops the Mac sleeping when idle, -d keeps the display on, -m keeps the disk awake, -s keeps the whole system awake while on power (caffeinate manual). For agents and builds, -i is enough; add -d only if you want to watch the screen.

2The setting, without Terminal

On a MacBook: System Settings → Battery → Options, then turn on Prevent automatic sleeping on power adapter when the display is off. On a desktop Mac: System Settings → Energy, then Prevent automatic sleeping when the display is off (Apple Support).

This works only while the Mac is on power, and it uses more energy, so turn it off when you no longer need it.

3Closing the lid

A MacBook goes to sleep when you close the lid, and caffeinate doesn't change that. It keeps working with the lid closed only in closed-display mode: connected to power and an external display, with a keyboard and a mouse or trackpad (Other World Computing).

You may find sudo pmset -a disablesleep 1 online. It stops all sleep, even in a bag, where a closed MacBook running an agent can get very hot. If you use it, undo it with sudo pmset -a disablesleep 0 and never run a sudo line you don't understand.

4Apps that do it for you

If you'd rather click than type, menu bar apps wrap the same system switch:

  • Amphetamine (free, Mac App Store): sessions with timers, "while an app is running" and more.
  • KeepingYouAwake (free, open source): one click in the menu bar.

5For agents running while you're away

  • Keep the Mac on power, and on desktop Macs check Wake for network access under Energy, so Screen Sharing and SSH can still reach it.
  • Run the agent inside caffeinate -i, so the Mac sleeps again by itself when the agent finishes.
  • A Mac mini makes a quiet always-on AI machine; see the Mac mini AI server guide.
  • Before you leave an agent running, check what it may touch. If it ever ran a command you didn't expect, do the Mac check.

6Questions

How do I keep my Mac awake for Claude Code?
Start it with caffeinate -i claude instead of claude. The Mac stays awake while Claude Code runs and can sleep again when it ends. Keep the Mac on power for long sessions.
What's the Terminal command to keep a Mac awake?
caffeinate -i keeps it awake until you press Control-C; caffeinate -i -t 3600 for an hour; caffeinate -i followed by any command, for as long as that command runs.
Can I keep my MacBook awake with the lid closed?
Only in closed-display mode: on power, with an external display, keyboard and mouse. Otherwise closing the lid puts it to sleep, whatever caffeinate says.
Does caffeinate drain the battery?
It stops sleep, so the Mac uses as much power as the work it's doing. On battery, use a time limit with -t, or plug in.
Will my Mac keep working when the screen locks?
Yes, as long as the Mac itself isn't asleep. The display can turn off and the screen can lock while caffeinate or the sleep setting keeps the Mac running.

Sources

  1. caffeinate manualss64.com
  2. Apple Supportsupport.apple.com
  3. Other World Computingeshop.macsales.com
  4. Amphetamineapps.apple.com
  5. KeepingYouAwakegithub.com

More Mac safety