For agents · Claude Code

Keep your Mac awake
while Claude Code works.

Claude Code runs on your Mac, so when the Mac sleeps, Claude stops mid-task. Midnight Oil keeps the Mac awake for exactly as long as Claude is working, even with the lid closed, then lets it sleep.

The short answer

Start Claude through Midnight Oil with midnightoil run --lid-closed -- claude and the Mac stays awake until you quit the session. Or just tell Claude “keep my Mac awake until you're done,” or add one line to CLAUDE.md so it does that for every long job.

Claude Code at a glance

Runs whereIn your terminal, on your Mac
Interactiveclaude
Unattendedclaude -p "…" prints the result and exits
Instructions fileCLAUDE.md in the repo, or ~/.claude/CLAUDE.md for every project
Needs your Mac awake?Yes. Claude Code on the web runs in Anthropic's cloud and doesn't.

What happens when your Mac sleeps mid-run

A sleeping Mac freezes every process, Claude Code included. The request Claude was streaming from Anthropic's API is cut off, the shell commands it started stop where they were, and nothing moves until you open the lid. You come back to the step it was on at midnight, not a finished task.

Claude Code doesn't hold the Mac awake on its own. It's a terminal program; macOS sees an idle machine and sleeps on its usual schedule, or the moment the lid closes.

Just ask Claude Code

With Midnight Oil installed, Claude Code can run the midnightoil command for you. Say it in your own words:

You sayClaude Code runs
“Keep my Mac awake until you're finished. I'm closing the lid.”midnightoil start --lid-closed
“Run the full test suite and keep the Mac awake till it's done.”midnightoil run -- npm test
“Stay awake until 7am, then let it sleep.”midnightoil start --until 7am

The session shows up in the menu bar like one you started yourself, so you can see it, extend it, or end it with a click.

Make it automatic

Add this line to CLAUDE.md and Claude Code keeps the Mac awake for every long command without being asked:

For CLAUDE.mdClaude Code reads midnightoil --help for the rest.
For commands that take more than a few minutes, run them through `midnightoil run -- <command>` so the Mac stays awake until they finish.

Running Claude Code unattended

For long jobs, claude -p runs a single prompt and exits, which makes it easy to wrap: the session lasts exactly as long as Claude does, and midnightoil exits with Claude's exit code, so scripts can tell whether it worked.

For an interactive session you'll walk away from, start claude through midnightoil run. The terminal works as usual; when you quit Claude, the Mac goes back to sleeping normally.

Keep the Mac awake for the whole session

midnightoil run --lid-closed -- claude

Or for one unattended run

midnightoil run --lid-closed -- claude -p "fix the failing tests and commit"

Lid closed, battery safe

Add --lid-closed and Claude Code keeps working with the laptop shut. It needs a one-time approval in Midnight Oil's Settings › Closed Lid, and normal sleep comes back the moment the session ends, even if the app quits or crashes.

On battery, sessions can end when you unplug or drop below a level you pick, and an alarm sounds if the lid closes during a closed-lid session. Add --allow-display-sleep to let the screen go dark while the Mac keeps working. The full setup is in how to run AI agents overnight.

Questions

Does Claude Code keep running when my Mac sleeps?

No. Sleep pauses every process, so Claude stops mid-request and picks nothing back up until you wake the Mac. Keep the Mac awake for the session with midnightoil run -- claude, or ask Claude to run midnightoil start.

Can I close my MacBook while Claude Code works?

Yes, with Midnight Oil's closed-lid mode: run midnightoil run --lid-closed -- claude. Closed-lid mode needs a one-time approval in Settings › Closed Lid, and sessions can end on their own below a battery level you choose.

What do I put in CLAUDE.md?

One line: For commands that take more than a few minutes, run them through `midnightoil run -- <command>` so the Mac stays awake until they finish. Put it in ~/.claude/CLAUDE.md to cover every project.

Do I need this for Claude Code on the web?

No. Claude Code on the web runs in Anthropic's cloud, so your Mac can sleep. You only need it when Claude Code runs on your own Mac.

Let Claude Code work the night shift.

Free. No account, no subscription.

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