Start the agent through caffeinate: caffeinate -i claude for Claude Code, caffeinate -i codex for the Codex CLI or caffeinate -i agent for Cursor’s CLI. The Mac then stays out of idle sleep for as long as that process runs, and the request ends when the agent exits. For an agent that is already running, use caffeinate -i -w PID. Keep a MacBook’s lid open and connect power.
Why a long agent run stops
macOS puts a Mac to sleep after a period without keyboard, mouse or trackpad input, using the timers in System Settings, unless an app asks it to stay awake. An agent that edits files and runs tests in Terminal produces output, not input. If you start a long task and walk away, the Mac can reach its sleep timer in the middle of the run.
While the Mac sleeps, the agent does not work, and its connection to the model’s online service can drop. What happens when the Mac wakes depends on the tool and on the task; we have not tested how each tool recovers. The documentation of Claude Code, Codex and Cursor’s CLI that we read on November 30, 2026 says nothing about Mac sleep, so the awake request has to come from the Mac side.
The built-in way to make that request is caffeinate. It is free, needs no install and is described in Apple’s caffeinate manual. For other ways to keep a Mac awake, see the overview of keep-awake methods.
Start the agent under caffeinate
Put caffeinate -i in front of the command you already use to start the agent. The manual says that when you give caffeinate a command, it holds the request for as long as that command runs.
| Tool | Interactive session | One task, then exit |
|---|---|---|
| Claude Code | caffeinate -i claude | caffeinate -i claude -p "your task" |
| Codex CLI | caffeinate -i codex | caffeinate -i codex exec "your task" |
| Cursor CLI | caffeinate -i agent | caffeinate -i agent -p "your task" |
Replace your task with your prompt and add the options you normally use after the tool’s name. These tools change often: if a command is not found, check the tool’s current documentation for the new name.
-i only prevents idle system sleep. The display can still turn off and the screen can still lock, which is fine for an agent that works on its own. On a MacBook Pro 14" (2021) with macOS 27.2.1 we ran caffeinate -i claude --version: Claude Code printed its version (2.1.331) and the command returned right away with exit code 0, so caffeinate passes control back as soon as the tool exits.
Attach to an agent that is already running
If the agent is already working and you do not want to restart it, open a second Terminal tab and find its process ID:
pgrep -lx claudepgrep -lx lists the processes with exactly that name and their PIDs, for example 816 claude. Each open session is a separate process, so you may see several: pick the one doing the long task, or compare with the PID column in Activity Monitor. For the other tools try pgrep -lx codex or pgrep -lx agent; if nothing appears, the process may have a different name, so look it up in Activity Monitor. Then attach caffeinate to that PID:
caffeinate -i -w 816The agent keeps running as before; caffeinate only waits for that process. Leave the second tab open. When the agent exits, caffeinate releases the request and exits too. The caffeinate guide covers -w and how to stop a request early.
To cover everything you start in one Terminal tab, run caffeinate -i -w $$ & in that tab. $$ is the PID of the shell itself, so the request lasts until you type exit or close the tab.
An agent inside an editor such as Cursor does not start from your Terminal command. You can attach to the editor’s PID the same way, but the Mac then stays awake until you quit the whole editor. A timed request is often simpler: caffeinate -i -t 14400 keeps the Mac out of idle sleep for four hours (the value is in seconds).
Check that the request is active
While the agent works, run this in another tab:
pmset -g assertionsUnder the list of processes, look for a caffeinate line with PreventUserIdleSystemSleep and, in the details, the PID of your agent. This is the output from our test, shortened:
pid 2214(caffeinate): ... PreventUserIdleSystemSleep named: "caffeinate command-line tool"
Details: caffeinate asserting on behalf of Process ID 2213In that test on November 30, 2026 (macOS 27.2.1) we attached caffeinate -i -w to a 30-second test process. The entry named that process’s PID; after the process ended, caffeinate exited with code 0 and the entry was gone. Run the command again after your agent finishes to see the same. If the Mac still does not sleep afterwards, the guide to finding what keeps a Mac awake explains the rest of the output.
Lid, battery, screen lock and SSH
- Lid: do not rely on caffeinate to keep a MacBook awake with the lid closed. Apple describes closed-lid use with a power connection, an external display and an external keyboard and mouse. For an overnight run on the laptop alone, leave the lid open. Details are in the lid-closed guide.
- Battery: a long run uses charge the whole time, and an empty battery ends it. Connect power. The
-sflag, which prevents system sleep, works only on AC power according to the manual. See keeping a Mac awake on battery. - Screen lock: locking the screen does not stop a running process, and
-ilets the display turn off. Keep your usual password protection. The locked-screen guide shows how to test this before you leave. - SSH: if the agent runs on another Mac that you reach over SSH, the caffeinate request has to run on that Mac, not on the one you type on. The SSH guide separates the host from the client.
When a stop time or a schedule helps
For one agent run, the commands above are enough: they are free, built into macOS and end with the agent. A menu-bar app helps when the Mac should stay awake during set hours, for example if you start agent tasks every weekday evening, or when it should stop at a clock time whether or not you remember.
Mouse Jiggler has Schedule (From and To hours on chosen days), Deactivate (stop after a duration or at a time) and a Prevent Sleep While Active setting; some of these controls require PRO. An app session does not know when your agent finishes: it ends at the time you set, so check the agent’s result yourself. The scheduling guide shows a short test session. This website is published by the app’s developer.
Download Mouse Jiggler on the Mac App Store. Free to download, with optional PRO features.
Check the agent’s work before you close the session.
Read what the agent changed and run your tests. For timers and other flags, use the caffeinate reference.
Review caffeinate commandsView Mouse Jiggler on the Mac App Store · Free to download, with optional PRO features.



