Keep Your Mac Awake While Claude Code or Codex Runs
Keep your Mac from sleeping mid-task while Claude Code or Codex runs: caffeinate, hooks or activity detection, limit resets, lid closed and battery.
Use caseUpdated 11 min read
On this page
The quickest way to keep your Mac awake while Claude Code or Codex runs is to start the agent with macOS's built-in caffeinate. In Terminal, type caffeinate -i claude (or caffeinate -i codex), and the Mac won't idle-sleep until the agent exits. The catch is that it stays awake the whole time the agent is open, including hours spent waiting for your next prompt. If you want the Mac awake only while the agent is working, and free to sleep a few minutes after it goes quiet, use an app that tracks agent activity, such as our Glowmoth keep-awake app.
We make Glowmoth, so weigh our view accordingly. We've tried to be fair to the other tools here.
Why a long agent run stops when your Mac sleeps
macOS idle-sleeps when there has been no keyboard, mouse or trackpad input for the time in your sleep settings, unless an app asks it to stay awake. A busy Terminal process doesn't count as input. An agent can be editing files and running tests, and the Mac still treats you as away once the timer runs out.
While the Mac sleeps, the agent's process stops running and its network connections drop. Requests to the model stall or fail, and the task stops partway through.
The built-in fix is to turn off automatic sleep. Open System Settings and search for "sleep" to find the options for your Mac (see Mac sleep settings explained). That works, but the Mac then stays awake every night, whether an agent is running or not.
What a good setup looks like
- The Mac stays awake while the agent is working, whether you're at the desk or not.
- It can sleep soon after the agent finishes or stops to wait for you, instead of running all night.
- If work should continue after a usage-limit reset, the Mac is awake at the reset time.
- The display can sleep, because the agent doesn't need the screen.
- On a laptop, it stops before the battery runs flat and backs off when the Mac runs hot.
Three ways to keep the Mac awake for an agent
| Approach | Examples | Holds the Mac awake | Lets it sleep | Setup |
|---|---|---|---|---|
| Wrap the command | caffeinate -i, glowmoth run | From launch until the agent exits | Only after the agent exits | None, but you type it each time |
| Agent hooks | Caffeine (iannuttall), Awakado | During each turn, as the agent reports it | When the agent reports it's done or waiting | Hooks added to the agent's settings |
| Activity detection | Glowmoth | While the agent's processes are busy | A set cool-down after activity stops | Turn on once |
Hooks give the most precise picture, because the agent itself reports when a turn starts and ends. Activity detection needs no changes to the agent's config, but it infers "working" from process activity, so it uses a cool-down to ride out short pauses. Wrapping is the simplest and is free, but it can't tell working from waiting.
Quick fix: wrap the agent in caffeinate or glowmoth run
caffeinate (built in)
caffeinate -i claudeThis starts Claude Code and blocks idle system sleep until it exits. The display can still sleep. There's nothing to undo, because the hold ends when the command ends. For Codex, use caffeinate -i codex. To keep the display on too, add -d: caffeinate -di codex.
If the agent is already running, find its process ID in the PID column of Activity Monitor and attach to it:
caffeinate -i -w 12345This holds the Mac awake until process 12345 exits. Replace the number with your agent's PID.
What caffeinate can't do: it keeps the Mac awake while the agent waits at a prompt, closing a MacBook's lid still sleeps the Mac, and it has no battery or heat cut-off. More flags are in our caffeinate command guide.
glowmoth run (for scripted jobs)
Glowmoth includes a glowmoth command-line tool. To install it:
- Right-click the moth icon in the menu bar and choose Settings….
- Click the Advanced tab.
- Under Command Line, click Install.
This creates ~/.local/bin/glowmoth. If your shell can't find glowmoth, add that folder to your PATH:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrcThis adds ~/.local/bin to your PATH in new Terminal windows. To undo it, delete that line from ~/.zshrc.
Then wrap the script that runs your agent job:
glowmoth run --label "nightly agent job" -- ./agent-job.shThis keeps the Mac awake while the script runs and returns the script's exit code, so it fits inside other scripts. While the Glowmoth app is running, the job shows up in its panel with your label, and Glowmoth's battery and heat limits apply to it. If the app isn't running, glowmoth run holds its own power assertion, so the job is still covered. The display may sleep unless you add --display before the --.
Leases, for wiring your own hooks
If you'd rather connect agent hooks yourself, use a lease. Acquire it when a turn starts, send heartbeats while it runs, and release it when the turn ends:
glowmoth lease acquire --id agent-turn --label "Agent turn" --ttl 600
glowmoth lease heartbeat --id agent-turn --ttl 600
glowmoth lease release --id agent-turnA lease ends on its own if no heartbeat arrives within its TTL (here 600 seconds), so a crashed script can't keep the Mac up indefinitely. Glowmoth doesn't edit Claude Code or Codex settings for you. Every command and exit code is in the glowmoth command-line docs.
Keep your Mac awake only while Claude Code or Codex is working
This is the setup most people want for long agent runs. Glowmoth watches the agent's processes and holds the Mac awake only while they're busy.
- Right-click the moth icon in the menu bar and choose Settings….
- Click the Automation tab.
- Under Coding Agents, turn on Stay awake while coding agents work.
- Set Allow sleep after to 2, 5 or 15 minutes. The default is 5.
- Start Claude Code or Codex in Terminal as usual.
If you're setting up Glowmoth for the first time, choosing the AI Agents card in the welcome window turns on the same mode.


Glowmoth is part of MacSuite; you can try it free for 7 days.
How Glowmoth decides an agent is working
While an agent is running, Glowmoth checks its process tree every 5 seconds. A session counts as Working if, since the last check, it used at least 3% of a CPU core, started or finished a child process, or wrote to its terminal. Otherwise it's Waiting, and once the agent exits it's Finished.
Glowmoth keeps holding the Mac until the cool-down has passed since the last busy check. If the agent stops to ask you a question at 11 p.m. and you've gone to bed, Glowmoth lets go 5 minutes later (with the default) and the Mac sleeps on its normal schedule, instead of staying up until morning.
Agent sessions never keep the display on. The display sleeps as usual while the Mac stays awake.
Pick the cool-down
- 2 min: the Mac can sleep soonest after the agent goes quiet.
- 5 min (default): covers short pauses between steps.
- 15 min: use this if the Mac sleeps in the middle of a task.
Check that it's working
Click the moth icon to open the panel. It lists a reason such as "Claude Code working in" followed by the project folder name, and the menu bar icon shows the Automatic state (a ring-shaped bulb). The Coding Agents section in Settings lists each detected session as Working, Waiting or Finished.
To confirm from Terminal:
pmset -g assertionsThis lists the power assertions apps currently hold. It only reads, so it changes nothing. Glowmoth's entry starts with "Glowmoth:" followed by the reason.


Sleep or lock the Mac when the agent finishes
In Settings, click Sessions and find When a Session Ends. Set Then to Sleep the Mac or Lock the Screen. These actions run when the last reason ends on its own, which includes an agent's cool-down running out. They don't run when you stop things yourself. Under Also, you can choose Run a Shell Command or Run a Shortcut, for example to log when the job ended.
Per-agent control
Under Each Agent, set an agent to Keep Awake (the default), Track Only (listed but never holds the Mac) or Ignore (not detected). Track Only suits an agent you run casually and don't want keeping the Mac up.
The full reference is in the coding agents docs.
Which agents are detected
As of version 1.0.0, Glowmoth recognizes these command-line agents:
- Claude Code
- Codex
- Gemini CLI
- Aider
- Copilot CLI
- Cursor Agent
- OpenCode
- Amp
- Goose
- Qwen Code
- Crush
Glowmoth matches them by process name, or by the script or package path when the agent runs under node, bun, deno, python or ruby. Wrapper processes and sub-agents are folded into the agent's session, so they don't show up as separate entries.
Desktop app versions, such as Claude.app, aren't counted. If your agent isn't on the list, add its executable name in Settings ▸ Automation ▸ Processes. That holds the Mac awake whenever the process runs, busy or not. For a single job, glowmoth run works with any command.
Agent-aware keep-awake apps compared
As of November 2026:
| Tool | How it tracks the agent | Agents | Price | Worth knowing |
|---|---|---|---|---|
caffeinate | It doesn't; holds while the command runs | Any | Built into macOS | No battery, heat or lid handling |
| Caffeine (iannuttall) | Optional hooks for Claude Code and Codex; process-based Agent Watch as a default-off fallback | Claude Code, Codex, common agent processes | Free, MIT licensed | Version 0.1.0; macOS 14 or later; best-effort closed-lid mode that doesn't change pmset |
| Awakado | The agents' hooks, with working, needs you, usage limit, stalled and done states | 21 | Free; Pro $9.99 once for 3 Macs | Lid-closed mode is in Pro; wakes when a Claude Code usage limit resets |
| Keepresso | Agent detection | 20+ | Free, GPL-3.0 | Also has leases, a CLI and an MCP server |
| Glowmoth | Process activity plus a cool-down | 11 | Part of MacSuite, $349 one-time | No hooks, so no "needs you" state or notification |
- Pick
caffeinateif you run one agent at a time and quit it when it's done. - Pick a hook-based app (Awakado or Caffeine) if you want the app to know exactly when a turn ends or the agent needs you.
- Pick Glowmoth if you'd rather not change your agents' settings, you use several of the agents above, or you want battery, heat and closed-lid handling in the same app. If you also need an alert when the agent is waiting, see getting notified when Claude Code needs you.
Keep the Mac awake for a usage-limit reset
When Claude Code or Codex hits a usage limit, it can't continue until the limit resets. With activity detection on, the agent goes quiet and Glowmoth lets the Mac sleep after the cool-down, which is usually what you want. If you've set up work to continue at the reset, the Mac needs to be awake then.
Option 1: add a scheduled run yourself
This is the reliable way, because it holds the Mac from now until the reset.
- In Settings, click Automation and find Scheduled Runs.
- Click Add Run….
- Set Start at to the reset time.
- Choose a length in Stay awake for, such as 1 Hour.
- Leave Keep awake until then on. It appears when Glowmoth can't schedule a system wake, and keeps the Mac awake from now until the start time.
- Click Add Run.
You can also keep a plain session running past the reset with glowmoth until 05:30. A plain session keeps the display on by default; add --no-display to let it sleep.
Option 2: let a hook schedule the run
Pass the agent's limit message to glowmoth agent. The message is the one the agent prints, ending in something like "limit reached ∙ resets 5am (Europe/London)":
glowmoth agent --event <event-name> --message "…limit reached ∙ resets 5am (Europe/London)"Glowmoth reads the reset time from the message and schedules a run that starts 2 minutes before the reset and lasts 30 minutes. While that run holds the Mac, the panel shows "Agent limit reset until" followed by the end time. A newer message replaces the previous run. This needs an active trial or license.
From an agent hook, leave out --message and the command reads the hook's JSON from standard input. It always exits 0, so it never blocks the agent. Events other than a usage-limit message are accepted but ignored for now, and Glowmoth doesn't install the hook for you.
Two limits matter here:
- Glowmoth can't wake a sleeping Mac in a normal install, because macOS doesn't give the app the rights to do that. A hook-scheduled run only helps if the Mac is still awake at that time, which is why option 1 is safer.
- Glowmoth doesn't restart the agent or resend your prompt. It only keeps the Mac awake so a job that's set to continue can run.
More detail is in the scheduled runs docs, and our Claude Code usage guide explains how the limits work.
Lid closed and battery safety on long runs
Closing the lid
Closing a MacBook's lid sleeps it, even while caffeinate or Glowmoth's normal keep-awake is active. The built-in way around this is clamshell mode: connect power and an external display, then close the lid. Our clamshell mode guide covers the setup.
Without an external display, Glowmoth's closed-lid mode can keep a MacBook awake with the lid shut:
- In Settings, click Safety.
- Under Closed Lid, turn on Stay awake with the lid closed.
- Glowmoth installs a small helper and opens Login Items in System Settings. Allow the Glowmoth helper there.
The helper turns off sleep only while Glowmoth is actually keeping the Mac awake, for any reason, including a working agent. When the agent's cool-down ends, normal sleep returns and a closed MacBook goes to sleep. Sleep also returns immediately if the Mac runs hot or the battery drops below your floor, and within 30 seconds if Glowmoth stops responding. Closed-lid mode is available on laptops only. The closed-lid docs have the full details.
Battery and heat
Glowmoth pauses everything, agents included, when either of two safety limits trips. Both are on by default:
- Pause on low battery: below 20% while on battery (adjustable from 5% to 50%). It resumes when you plug in or the battery reaches 5 points above the floor.
- Pause when the Mac is running hot: after macOS reports a serious thermal state for 60 seconds. It resumes after 2 minutes at a normal temperature.
The panel shows Resume Anyway (30 min) if you need to override a pause briefly. While closed-lid mode is on, you can't turn either limit off. For an overnight run, plug the Mac in so the battery floor doesn't stop the job. Details are in the battery and heat docs.
Common mistakes
- Using the desktop app instead of the CLI. Glowmoth detects command-line agents only. Claude.app isn't counted.
- Leaving Glowmoth in Manual. Manual ignores agents, leases and every other automatic reason. Switch the panel back to Automatic.
- Forgetting you used Stop Everything. It suspends all automatic reasons, agents included, until you start a session again or relaunch Glowmoth.
- Letting the trial lapse. With no active trial or license, Glowmoth keeps nothing awake, and leases sent to the app don't hold the Mac either.
- Closing the lid with no plan. Use clamshell mode or closed-lid mode, or keep the lid open.
- Expecting a wake at the limit reset. The Mac must already be awake; add a scheduled run with Keep awake until then.
- Expecting
caffeinateto let the Mac sleep while the agent waits. It holds until the process exits. Quit the agent, or use activity detection. - Shell can't find
glowmoth. Add~/.local/binto your PATH, as shown above.