Guide

Keep your Mac awake for Claude Code, Codex and Cursor

Updated 11 August 2026 · 6 min read

The short answer: macOS decides when to sleep based on user input, not CPU work — so a long agent run looks idle and the Mac sleeps, killing it. Fix it in one of three ways: wrap the session in caffeinate -dimsu claude, wire caffeinate into Claude Code's hooks so it only holds during active turns, or flip a menu-bar switch that covers everything in that window.

Why this happens at all

It feels like a bug, but it's working as designed. macOS sleep timers are driven by human activity — keystrokes, trackpad movement, an app explicitly asking to stay awake. A terminal quietly burning CPU while an agent thinks does not count as activity.

So after your idle timer expires, the display sleeps. On Apple Silicon laptops the machine then slides toward standby, and that's where real damage happens: your process is suspended, open network connections drop, and the API stream the agent depends on dies. You come back to a half-finished run and a confused terminal.

This bites hardest on exactly the workloads people now run all day: long Claude Code sessions, Codex tasks, Cursor agents, big builds, model downloads, and test suites.

Option 1: the built-in caffeinate command

macOS ships with a tool for this and most people never hear about it. The cleanest form wraps the whole session:

caffeinate -dimsu claude

The Mac stays awake for exactly as long as that process lives, then sleep behaviour returns to normal on its own. No settings to undo. Here's what each flag actually does:

-dPrevent the display from sleeping
-iPrevent idle system sleep
-mPrevent the disk from sleeping
-sPrevent system sleep (on AC power)
-uSimulate user activity
-t 3600Hold for a fixed number of seconds

Other useful shapes — hold for two hours regardless of what you're running, or keep an already-running process alive by its PID:

caffeinate -dimsu -t 7200
caffeinate -dimsu -w 12345

The catch: you have to remember to do it, every time, before the run starts. Start the agent first and you're unprotected. Close the terminal tab and the assertion dies with it. And it does nothing for a closed lid.

Option 2: wire it into Claude Code hooks

If you want the Mac awake only while a turn is actually in flight, Claude Code's hooks can start and stop caffeinate around each turn — UserPromptSubmit to begin, Stop to release.

It's elegant, and it's the most power-efficient option. But be aware of the trade-off for unattended work: an agent loop has idle gaps between phases, and a per-turn hold lets the Mac sleep in exactly those gaps. For an overnight run you usually want the whole session held, not each turn.

Option 3: a menu-bar switch

The reason we built Awake: one click before you start, and everything in that window is covered — the agent, the build it kicks off, the download it triggers, the test run afterwards. Nothing to remember, nothing to type, no flags to look up.

It also handles the two things caffeinate genuinely can't:

 caffeinateHooksAwake
CostFreeFree$14.99 lifetime
Setup each timeType the commandOne-time configOne click
Covers the whole sessionYesPer turn onlyYes
Works with the lid closedNoNoYes, on power
Stops at low batteryNoNoYes, at 20%
Stops if the Mac gets hotNoNoYes

Being straight with you: if you're happy typing caffeinate -dimsu claude every time, it genuinely solves the core problem for free, and you should just use it. Awake is worth paying for when you want it to be automatic, and when you want the lid-closed and safety-cutoff behaviour that the command line can't give you.

What about the lid?

This is where people get burned, so here's the honest physics. A closed laptop traps heat against its own body — that's precisely why macOS sleeps on lid-close when you're unplugged. It's a safety decision, not an arbitrary limit.

Check that it's actually working

Don't trust it blindly — verify. This lists every active power assertion, so you can see exactly what's holding your Mac awake and which process owns it:

pmset -g assertions

One click, and the run finishes

Awake keeps your Mac awake for the whole session — lid open or closed — and stops on its own at low battery or if things get hot. Free 3-day trial, no card.

Download Awake for Mac

Common questions

Why does my Mac sleep while Claude Code is running?

Because macOS measures user input, not CPU load. An agent thinking for ten minutes with no keystrokes looks idle, so the display sleeps and the system follows — suspending your process and dropping network connections.

Does caffeinate work with the lid closed?

No. Closing the lid triggers a clamshell sleep that a standard assertion can't override on battery. On power, a clamshell-capable app can hold the Mac running with the lid shut.

Can I keep the Mac awake for a specific amount of time?

Yes — caffeinate -dimsu -t 7200 holds for two hours then releases. Awake does the same from the menu with 30-minute, 1-hour and 5-hour presets.

Will running overnight damage my Mac?

Lid open is normal and safe. The only real risk is a closed laptop on battery with nowhere to vent, which is why Awake stops automatically if the machine runs hot, at 20% battery, and after 8 hours.

Does this work for Codex, Cursor and Gemini CLI too?

Yes. None of this is specific to one agent — it's about stopping macOS from sleeping. Any long-running process benefits: agents, builds, model downloads, test suites, renders.

Related: the complete caffeinate guide · keeping your Mac running with the lid closed · Awake vs Amphetamine · all comparisons