Keep Codex CLI running when your Mac sleeps
How to check whether your Mac is holding off sleep for Codex, and how to keep long Codex runs alive, lid open or closed.
By Harsh Soni, maker of Latte · Updated
OpenAI's Codex CLI can run long tasks on its own, but only while your Mac stays awake. If you come back to a Codex run that stopped halfway, sleep is the usual reason. Here's how to check, and how to fix it.
Step 1: See whether anything is holding the Mac awake
Start a Codex task, then in another terminal run:
pmset -g assertions | grep -i -E "idle|sleep"If you see a PreventUserIdleSystemSleep entry from Codex, your terminal or another tool, idle sleep is covered while it's there. If you don't, the Mac will sleep after its usual idle time and the run will stop.
Step 2: Hold off idle sleep for the run
The simplest fix is to start Codex through caffeinate, so the hold lasts exactly as long as Codex does:
caffeinate -i codexYou can make that your default with a shell alias, for example alias codex="caffeinate -i codex" in ~/.zshrc. The catch: it holds the Mac awake for the whole session, including while Codex is idle and waiting for you.
Step 3: Decide about the lid
caffeinate doesn't stop the Mac sleeping when you close the lid. If you want Codex to keep going with the MacBook closed, you need clamshell mode (power plus an external display), the pmset disablesleep setting, or a tool that handles it. All of them are compared in how to keep a coding agent running with the lid closed; the options are the same for Codex.
Using Latte with Codex
Latte (which I make) connects to Codex with one click and backs up its config first. With Codex it:
- holds the Mac awake while Codex is actually working, lid open or closed, and lets it sleep when it's done
- pauses Codex if the Mac gets hot, the battery hits your floor or the network drops, and resumes it afterwards
- tells you on your phone when a run finishes, and answers
/statusfrom Telegram
Answering permission prompts from your phone works with Claude Code today. For Codex, Latte sends status and finished alerts, so you know when to check back.
Common questions
- Does Codex CLI keep my Mac awake?
- Check on your own Mac: run pmset -g assertions while Codex is working. If no PreventUserIdleSystemSleep entry appears, nothing is holding off sleep and the run will stop when the Mac idles.
- How do I stop my Mac sleeping while Codex runs?
- Start it with caffeinate -i codex. The Mac stays awake until Codex exits. This does not cover closing the lid.
- Can Codex keep running with the MacBook lid closed?
- Yes, with clamshell mode (power plus an external display), the pmset disablesleep setting, or a tool like Latte that holds the Mac awake only while Codex is working.
Latte keeps Claude Code, Codex and Grok working with your MacBook closed, sends approvals to your phone, and lets the Mac sleep the moment the work is done.