How to keep Codex CLI running overnight on a Mac

Your Mac went to sleep mid-run and Codex CLI stopped with it — that's macOS, not Codex. The fix is to stop the Mac from sleeping while Codex is actually working, then let it sleep again shortly after it goes quiet — within your grace window, 90 seconds by default.

To keep your Mac awake while Codex CLI runs: the free way is macOS's built-in caffeinate -i — run it in Terminal before you start, and stop it yourself when the run ends. The automatic way is AgentBarista, which detects Codex working and lets your Mac sleep when the run finishes.

Why this happens

macOS doesn't know Codex is still working.

Codex CLI spends a lot of its time waiting on a model response, not burning CPU. But CPU isn't what macOS checks: it goes by keyboard and mouse input plus the power assertions running processes hold, so a quiet terminal on a long Codex run and a busy one are treated the same — neither counts as activity. So your default sleep timer (or just closing the lid) cuts the process off mid-task, and you come back to a half-finished run or a dead SSH session.

This isn't a Codex bug. The same thing happens with Claude Code, Cursor, and any CLI agent that runs long and quiet. The fix has nothing to do with Codex itself — it's about keeping the Mac awake for the duration of the run, then giving sleep back immediately after.

The free way

You can fix this for free right now.

If you only run Codex overnight occasionally, a free tool is a completely reasonable choice — no need to install anything else.

  1. Run caffeinate -i alongside Codex — macOS's own built-in command. Open a second terminal tab and run caffeinate -i before you start your Codex session; it blocks idle sleep until you stop it (Ctrl+C) or close the tab.
  2. Or install Amphetamine from the Mac App Store — free, lets you trigger keep-awake when a process is running, with a manual allowlist you set up yourself.
  3. Turn off "Prevent automatic sleeping" in Energy Saver settings — works, but it also keeps the Mac awake when Codex isn't running at all, which is worse for your battery and your fans than either option above.

The catch with all three: none of them know what Codex CLI is. They keep the Mac awake whether or not anything is actually happening, and you have to remember to turn them off — or your battery pays for it overnight.

If you run Codex a lot

What AgentBarista does differently for Codex CLI.

Codex CLI is one of the agents AgentBarista watches for out of the box. Instead of a blanket "stay awake" switch, it tracks whether Codex is actually doing something — CPU use from the Codex process and what's showing on screen — and only holds sleep off for that.

Knows it's Codex, specifically

Detects the Codex CLI process directly, not "any terminal with activity" — so it doesn't keep your Mac awake for an unrelated tab.

Stays awake through thinking time

Codex waiting on a model response looks idle to macOS. AgentBarista still counts that as "working" and keeps sleep off through it.

Lets go when it's done

When the run finishes, normal sleep comes back within your grace window — 90 seconds by default — so your Mac isn't held awake all night for no reason.

It also won't run your battery dead while you're not looking: a battery cutoff stops keep-awake below a threshold you set, and there's a one-command emergency restore (./reset-sleep.sh equivalent in the app) if anything ever gets stuck. Free tools don't do that part — that's the part you're paying for.

Private by design

What it actually looks at.

Getting it

$19/year, locked in for as long as you stay subscribed. Cancel any time.

AgentBarista Personal License — one Mac, $19/year
  • Runs as a menu-bar app — pick a mode, get back to work
  • Battery cutoff on by default (20%), plus a one-click emergency restore
  • Signed and notarized by Apple
Subscribe — $19/yr

$19/year if you subscribe before September 15 — locked in for as long as you stay subscribed. New subscribers after that pay $29/year. 30-day money-back guarantee — one email, full refund.

Download free 14-day trial

The download is the trial — the full app, free for 14 days, no card needed. Need more Macs? 5-Mac Pack — $129/year, flat.

More agents

Also running Cursor, Gemini CLI, or another agent?

Same Mac-goes-to-sleep problem, different tool. Each of these guides covers the fix for that agent specifically: