Just tell your agent
to keep your Mac awake.
Midnight Oil comes with a midnightoil command, so Claude Code, Codex, and Cursor can run it for you. Say what you want in plain words. Your agent turns it into a command, and your Mac stays awake for exactly as long as the work takes, even with the lid closed.
› Run the billing migration. Keep my Mac awake until it's done, I'm closing the lid. ⏺ Bash(midnightoil run --lid-closed -- npm run migrate) ⎿ Keeping your Mac awake: While “npm” runs Stays awake with the lid closed Migrating 1,284 invoices… (running) ⏺ Running. Your Mac stays awake until it's done, lid closed or not, then sleeps as usual.
Say it however you like
Your agent works out the command. A few ways to put it:
| You say | Your agent runs |
|---|---|
| “Keep my Mac awake until 7am.” | midnightoil start --until 7am |
| “Run the tests and keep the Mac awake till they're done. I'm closing the lid.” | midnightoil run --lid-closed -- npm test |
| “Stay awake for two hours, but let the screen go dark.” | midnightoil start --for 2h --allow-display-sleep |
| “Is anything keeping my Mac awake?” | midnightoil status |
| “OK, let it sleep.” | midnightoil stop |
Every session shows up in the menu bar like one you started yourself, so you can see what's keeping the Mac awake, extend it, or end it with a click.
Or make it automatic
Add one line to your CLAUDE.md, AGENTS.md, or Cursor rules, and your agent keeps the Mac awake for every long job without being asked:
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.
The commands
For your agent, your scripts, or you.
midnightoil run -- <command> | Runs the command and keeps the Mac awake until it finishes. Exits with the command's own status, and prints its messages to stderr so the command's output still pipes cleanly. |
midnightoil start | Keeps the Mac awake until you stop it. Add --for 90m, --until 6:30pm, or --while-pid 4242 to end it on its own. |
midnightoil status | Says whether the Mac is being kept awake, by what, and until when. Add --json for scripts. |
midnightoil stop | Ends the current session, the same as End Session in the menu. |
Options
--lid-closed | Keep running with the lid shut. Needs the one-time helper approval in Settings › Closed Lid. |
--allow-display-sleep | Let the screen turn off while the Mac keeps working. Best for overnight runs. |
--json | Print the session as JSON. |
Leave an option out and your defaults from Midnight Oil's settings apply. If the app isn't running, start and run open it in the background. When two agents keep the Mac awake at once, they share one session that lasts until the last one finishes.
Why not just caffeinate?
It's built in, and for a quick job on an open laptop it's fine. It stops short where agents need it most: a closed lid, a battery running low, and several jobs at once. More in the caffeinate guide.
| Midnight Oil | caffeinate | |
|---|---|---|
| Your agent can run it | Yes | Yes |
| Keeps working with the lid closed | Yes, and sleep comes back on its own | No |
| Ends on low battery or unplug | Yes, using your settings | No |
| Clock times | --until 6am | Seconds only, with -t |
| Agents running in parallel | One shared session until the last finishes | One assertion per process |
| Shows it's running | Flame and countdown in the menu bar | Nothing; check pmset -g assertions |
| Also a menu bar app | Yes, with schedules and triggers | No |
| Install | Comes with Midnight Oil | Built in |
Get the command
It comes with Midnight Oil. Install with Homebrew and it's on your PATH:
brew install --cask coreyhaines31/tap/midnightoil
Installed the app from the DMG? Link the command once:
sudo mkdir -p /usr/local/bin
sudo ln -sf "/Applications/Midnight Oil.app/Contents/MacOS/midnightoil" /usr/local/bin/midnightoil
Closed-lid mode needs a one-time approval: open Midnight Oil's Settings › Closed Lid and click Install. The full reference is in the README.
Set up for your agent
Questions
Can I ask Claude Code to keep my Mac awake?
Yes. With Midnight Oil installed, ask in plain words, like “keep my Mac awake until the migration is done, I'm closing the lid.” Claude Code runs the midnightoil command, and the Mac stays awake until the job finishes, lid closed included.
Do I have to teach my agent the commands?
No. Agents read midnightoil --help like any other command. To make it automatic, add one line to CLAUDE.md or AGENTS.md telling them to run long commands through midnightoil run.
Which agents work with it?
Any agent that can run terminal commands: Claude Code, Codex, Cursor, Gemini CLI, and the rest. You can also use the command yourself or in scripts.
How is midnightoil different from caffeinate?
Both hold the same macOS power assertion. midnightoil adds closed-lid mode, your battery safety settings, clock times like --until 6am, a countdown in the menu bar, and one shared session for agents running in parallel.
What happens if two agents keep the Mac awake at once?
They share one session, which lasts until the last command finishes. One agent finishing never lets the Mac sleep under the other.
How do I get the midnightoil command?
Install Midnight Oil with Homebrew (brew install --cask coreyhaines31/tap/midnightoil) and it's on your PATH. If you installed the app from the DMG, link it once with sudo ln -sf "/Applications/Midnight Oil.app/Contents/MacOS/midnightoil" /usr/local/bin/midnightoil.
Let your agent handle the night shift.
Free. No account, no subscription.
macOS 14 or later · Apple Silicon and Intel · brew install --cask coreyhaines31/tap/midnightoil