Guide · Updated 2026-10

Run Claude Code, Codex, or Cursor agents overnight

You start a long task at 11 PM, go to bed, and wake up to a Mac that fell asleep at 11:15 with the agent frozen mid-step. Here's why that happens and how to set things up so the run finishes.

The short answer

Plug in, keep the Mac awake for the length of the run, let the display sleep, and make sure the agent won't stop to ask for permission. The quickest version: caffeinate -i in a tmux session with the lid open, midnightoil run --lid-closed -- claude, or a Midnight Oil session while Terminal is running or until 7 AM, with the lid closed if you like.

Why agents stop overnight

macOS puts a Mac to sleep after a few idle minutes. An agent working in a terminal doesn't count as activity: there's no keyboard or mouse input, so the idle timer runs out on schedule. When the Mac sleeps, it suspends every process and drops network connections. The agent's request to its model breaks mid-step, an SSH session disconnects, and the run sits frozen until morning.

This catches people because the agent was clearly busy. To macOS, busy isn't the same as active.

Before you leave it running

  1. Plug in. A long run on battery can drain a laptop before morning. If you can't, set a battery floor.
  2. Keep the Mac awake for the length of the run. Not forever: a setting you forget to undo means a MacBook that never sleeps again. The options are below.
  3. Let the display sleep. The screen doesn't need to be on for the agent to work.
  4. Decide about the lid. Open is simplest. Closed needs clamshell mode or a closed-lid mode.
  5. Remove the approval prompts the task will hit. An agent that stops to ask at 1 AM waits until you're up. Grant what the task needs before you go, on a branch or in a sandbox where that's safe.
  6. Give it a clear finish line. “Run the tests after each step and open a pull request when they pass” gives you something to review in the morning.

The Terminal way: tmux and caffeinate

tmux new-session -d -s agent 'caffeinate -i claude'
tmux attach -t agent       # check on it later

caffeinate -i holds off idle sleep for as long as the agent runs, and tmux keeps both alive if you close the Terminal window. Swap claude for codex or whatever starts your agent. It doesn't survive a closed lid, and nothing shows it's still running. More in the caffeinate guide.

Midnight Oil's midnightoil command works the same way and covers the lid. It ends the session when the agent exits, and the menu bar shows it's still going:

tmux new-session -d -s agent 'midnightoil run --lid-closed -- claude'

Agents can use it too. Add a line to your CLAUDE.md or AGENTS.md: “Run commands that take more than a few minutes through midnightoil run -- <command>.” Setup for each agent: Claude Code, Codex, Cursor, Gemini CLI, and more.

The menu bar way: Midnight Oil

Midnight Oil is free and was built for this:

  • While App Is Running › Terminal (or Cursor, iTerm, Ghostty): the Mac stays awake until that app quits.
  • Keep Awake Until › 7:00 AM: awake through the night, then back to normal.
  • Stay awake with lid closed: close the laptop. Sleep comes back automatically when the session ends.
  • Allow display sleep: the screen goes dark, the Mac keeps working.
  • End sessions when the battery is low, and an alarm if the lid closes on battery.
  • Schedules: an “Overnight agents” schedule from 11 PM to 7 AM, only while plugged in, so you don't have to remember at all.

Agents on a Mac you don't sit at

If the agent runs on a Mac mini or a desk Mac you reach over SSH or screen sharing, the simplest fix is permanent: System Settings › Energy › Prevent automatic sleeping when the display is off. See how to stop your Mac from sleeping.

Running agents across several Macs for a team? Midnight Oil for Teams deploys the app and its rules with your MDM, and shows which Macs are awake and working.

Questions

Why does Claude Code stop when my Mac goes to sleep?

Sleep suspends every process and drops network connections. The agent's connection to its model breaks mid-step, and nothing happens until the Mac wakes. Some tools retry when it does; many don't, and you come back to an error or a half-finished change.

How do I keep my Mac awake while Claude Code runs?

In Terminal, run the agent inside tmux with caffeinate, for example tmux new-session -d -s agent 'caffeinate -i claude'. Or use Midnight Oil: start a session While App Is Running › Terminal, so the Mac stays awake until the terminal quits, or Keep Awake Until 7:00 AM. From the terminal, midnightoil run -- claude keeps the Mac awake until the agent exits, even with the lid closed if you add --lid-closed.

Can I close my MacBook while an AI agent runs?

Not by default: closing the lid sleeps the Mac, even with caffeinate running. Use an external display (clamshell mode), sudo pmset -a disablesleep 1 (and undo it after), or Midnight Oil's Stay awake with lid closed, which restores sleep on its own when the session ends.

Does an overnight agent run need the screen on?

No. The display can turn off while the Mac keeps working. It saves power and keeps a laptop cooler, which matters on long runs.

Will my agent wait for me to approve things overnight?

If it's set to ask before running commands or editing files, yes: it pauses at the first prompt and waits until morning. Before you leave, give the agent the permissions the task needs, in a project or branch where that's safe.

Give your agents the night shift.

Free. No account, no subscription.

macOS 14 or later · Apple Silicon and Intel · brew install --cask coreyhaines31/tap/midnightoil