Keep your Mac awake
while Codex CLI works.
OpenAI's Codex CLI runs on your Mac, so it stops when the Mac sleeps. Midnight Oil keeps the Mac awake while Codex works, lid open or closed, and lets it sleep the moment Codex is done.
The short answer
Wrap the run: midnightoil run --lid-closed -- codex exec "…". For an interactive session, midnightoil run -- codex. Or add one line to AGENTS.md and Codex keeps the Mac awake for every long command on its own.
Codex CLI at a glance
| Runs where | In your terminal, on your Mac |
| Interactive | codex |
| Unattended | codex exec "…" runs one task and exits |
| Instructions file | AGENTS.md in the repo, or ~/.codex/AGENTS.md for every project |
| Needs your Mac awake? | Yes for the CLI and IDE extension. Codex cloud tasks run on OpenAI's servers and don't. |
What happens when your Mac sleeps mid-run
When the Mac sleeps, Codex freezes with it: the model request in flight drops, the command it was running stops, and the task waits for you to come back. codex exec in a script or cron job is hit hardest, because nobody is watching when it stalls.
Codex doesn't ask macOS to stay awake. Idle sleep and a closed lid stop it like any other terminal program.
Just ask Codex CLI
With Midnight Oil installed, Codex CLI can run the midnightoil command for you. Say it in your own words:
| You say | Codex CLI runs |
|---|---|
| “Keep the Mac awake while you work through this. I'm heading to bed.” | midnightoil start --allow-display-sleep |
| “Build the release and keep my Mac awake until it finishes.” | midnightoil run -- make release |
| “I'm closing the lid. Keep going.” | midnightoil start --lid-closed |
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 AGENTS.md and Codex CLI keeps the Mac awake for every long command 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.
Running Codex CLI unattended
codex exec runs one task without the interactive UI, which is the cleanest thing to wrap: Midnight Oil holds the Mac awake from the first token to the last, exits with Codex's exit code, and prints its own messages on stderr, so Codex's output still pipes cleanly.
Several Codex runs at once share one session that lasts until the last one finishes, so one agent finishing never lets the Mac sleep under another.
Keep the Mac awake for the whole session
midnightoil run --lid-closed -- codex
Or for one unattended run
midnightoil run --lid-closed -- codex exec "migrate the API client to v2"
Lid closed, battery safe
Add --lid-closed and Codex CLI 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 Codex keep working when my Mac sleeps?
Not the CLI or the IDE extension: sleep pauses them mid-task. Codex cloud tasks keep going because they run on OpenAI's servers. For local runs, wrap them with midnightoil run -- codex exec "…".
What goes in AGENTS.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. ~/.codex/AGENTS.md applies it to every project.
Can I run several Codex jobs overnight?
Yes. Parallel midnightoil run sessions join one session that lasts until the last job finishes.
Will it drain my battery?
Only as long as Codex runs, and you can set sessions to end when you unplug or when the battery drops below a level you pick.
Using another agent?
Let Codex CLI work the night shift.
Free. No account, no subscription.
macOS 14 or later · Apple Silicon and Intel · brew install --cask coreyhaines31/tap/midnightoil