How to Keep Codex Running on a Mac Without Sleep


Before you start, you need:
Codex CLI is OpenAI's command-line coding agent and can perform development work directly from your terminal.
The important macOS command for this tutorial is caffeinate. It is built into macOS, so you do not need to install another package or keep a third-party utility running.
The simplest command is:
bash caffeinate -i
As long as that command is running, macOS is prevented from entering idle system sleep. Your display can still turn off, which is normally exactly what you want for a long-running Codex task.
When you run Codex locally from a terminal, the process depends on your Mac continuing to run.
For example, you may start:
bash codex
and ask Codex to:
If macOS enters system sleep while that work is happening, CPU execution and network activity may be suspended. The Codex process normally remains in memory, but it cannot continue doing useful work until the Mac wakes again.
Turning off the display is different from putting the Mac to sleep.
For long-running coding tasks, the ideal state is usually:
text Display off ↓ Mac awake ↓ Network active ↓ Codex continues running
You therefore usually want to prevent system idle sleep, not keep the display illuminated all night.
If Codex has not started yet, the cleanest solution is to launch it through caffeinate:
bash caffeinate -i codex
The -i option creates an assertion that prevents idle system sleep while the command is running.
The lifecycle becomes:
text caffeinate starts ↓ Codex starts ↓ Mac is protected from idle sleep ↓ Codex exits ↓ caffeinate exits ↓ Normal macOS sleep behavior returns
This is better than permanently disabling sleep because the protection exists only while you actually need it.
You can still pass normal arguments to Codex. For example:
bash caffeinate -i codex --help
For everyday use, this is the command worth remembering:
bash caffeinate -i codex
This is the most useful scenario when you have already started a long Codex task.
Suppose Terminal 1 currently contains:
bash codex
Codex is already editing files or running commands, and you realize that your Mac may go to sleep.
Do not stop Codex.
Open a second Terminal or iTerm2 window and run:
bash caffeinate -i
Leave that command running.
There may be no visible output. That is normal.
Your terminals now effectively look like this:
codex [Codex continues working]
caffeinate -i [No output; process remains running] `
As long as caffeinate -i remains active, idle system sleep is prevented.
This is the easiest solution when Codex is already in the middle of a task because it does not require restarting, interrupting, or reconnecting the Codex session.
When Codex finishes, return to Terminal 2 and press:
text Ctrl+C
That terminates caffeinate and restores normal sleep behavior.
Leaving caffeinate -i running works, but there is an even cleaner solution.
You can tell caffeinate to stay active only until the existing Codex process exits.
First, find the Codex process ID:
bash pgrep -fl codex
Example output:
text 48231 codex
The first number is the process ID, or PID.
Now run:
bash caffeinate -i -w 48231
Replace 48231 with your actual PID.
The -w option tells caffeinate to wait for that process to finish.
The lifecycle becomes:
text Existing Codex PID 48231 │ ├── caffeinate keeps Mac awake │ ▼ Codex finishes │ ▼ caffeinate automatically exits
This means you do not have to remember to return later and press Ctrl+C.
For an already-running Codex task, this is usually the best solution:
bash pgrep -fl codex
then:
bash caffeinate -i -w <PID>
For example:
bash caffeinate -i -w 48231
You may see multiple results from:
bash pgrep -fl codex
For example:
text 48231 codex 48245 codex 48302 codex
This can happen when you have multiple Codex sessions or related child processes.
Use ps to inspect a particular PID:
bash ps -o pid,ppid,etime,command -p 48231
Example output:
text PID PPID ELAPSED COMMAND 48231 47102 32:18 codex
Useful fields include:
If you are unsure which process belongs to your active task, the simpler alternative is still perfectly reasonable:
bash caffeinate -i
That prevents idle sleep globally while the command is running and avoids selecting the wrong PID.
You can inspect macOS power-management assertions with:
bash pmset -g assertions
When caffeinate -i is active, you should see an idle-sleep-related assertion in the output.
Look for information similar to:
text PreventUserIdleSystemSleep 1
You may also see a process entry identifying caffeinate as the owner of an assertion.
The exact output can vary between macOS versions, but the important point is that the relevant sleep-prevention assertion should be active.
You can also confirm that caffeinate itself is running:
bash pgrep -fl caffeinate
Example:
text 49120 caffeinate -i -w 48231
That confirms the helper process is still present.
For overnight or multi-hour Codex tasks, you usually do not need to keep the screen on.
Use:
bash caffeinate -i
rather than:
bash caffeinate -di
The important difference is that -d prevents the display from sleeping.
For coding agents, keeping the display illuminated provides little benefit and wastes energy.
A better setup is:
text Mac connected to power Display allowed to turn off MacBook lid left open caffeinate -i active Codex task running
The screen can become black while Codex continues running normally in the terminal.
caffeinate supports several sleep-related options.
bash caffeinate -i
This is the most useful option for Codex.
bash caffeinate -d
Use this only if you specifically need the screen to remain on.
bash caffeinate -di
Useful when you need to continuously watch output, but unnecessary for most unattended coding tasks.
Use -t followed by the number of seconds.
For example, keep the Mac awake for four hours:
bash caffeinate -i -t 14400
Because:
text 4 hours × 60 minutes × 60 seconds = 14400 seconds
This is useful when you know approximately how long a task should run.
bash caffeinate -i -w 48231
This is particularly useful for attaching sleep prevention to a Codex process that is already running.
Normally, if you start:
bash caffeinate -i
and then close that terminal window, the process may be terminated along with the shell session.
If you need the caffeinate process itself to survive that terminal session, you can use nohup:
bash nohup caffeinate -i >/tmp/caffeinate.log 2>&1 &
The shell should print a background job and PID similar to:
text [1] 50123
You can verify it with:
bash pgrep -fl caffeinate
To stop it later:
bash pkill caffeinate
Be careful with pkill caffeinate if you have multiple intentional caffeinate processes running, because it can terminate all matching processes.
For an existing Codex process, you can combine nohup with -w:
bash nohup caffeinate -i -w 48231 >/tmp/codex-caffeinate.log 2>&1 &
Now sleep prevention automatically disappears when PID 48231 exits.
However, keeping caffeinate alive does not automatically make the original Codex terminal session survive being closed. If you want long-running terminal sessions that can be safely detached and reattached, use a terminal multiplexer such as tmux in addition to caffeinate.
If you regularly run long Codex tasks, create a helper in your Zsh configuration.
Open:
bash nano ~/.zshrc
Add:
bash codex-awake() { caffeinate -i codex "$@" }
Save the file and reload your shell configuration:
bash source ~/.zshrc
You can now start Codex with:
bash codex-awake
Arguments are forwarded to Codex because of:
bash "$@"
For example:
bash codex-awake --help
For normal interactive use:
bash codex-awake
This gives you a simple rule:
text codex → normal Codex session codex-awake → Codex session protected from idle sleep
Avoid replacing the codex command itself with a shell function that accidentally invokes itself recursively. A separate name such as codex-awake is clearer and safer.
caffeinate solves the Mac sleep problem, but it does not solve every terminal-session problem.
For especially long tasks, consider combining it with tmux.
Start a tmux session:
bash tmux new -s codex
Run Codex:
bash codex
Then open another terminal and prevent sleep:
bash caffeinate -i
You can detach from tmux with:
text Ctrl+B, then D
Later, reconnect with:
bash tmux attach -t codex
This combination protects against two different problems:
`text caffeinate ↓ Prevents idle system sleep
+
tmux ↓ Keeps the terminal session alive when you detach `
For long unattended Codex work, this is a much more robust setup than depending on a normal terminal window alone.
caffeinate is useful, but it should not be treated as a guarantee that every possible interruption is prevented.
In particular, do not assume caffeinate -i protects you from:
caffeinate.For a MacBook running an unattended task, the safest practical setup is usually:
caffeinate -i or attach it with -w.tmux when terminal-session persistence matters.Avoid relying on caffeinate as a way to run a MacBook indefinitely with the lid closed. Lid-close behavior is different from ordinary idle sleep and depends on the Mac's hardware and operating conditions.
No.
Open another terminal and run:
bash caffeinate -i
That takes effect without restarting the existing Codex session.
For automatic cleanup, find the Codex PID:
bash pgrep -fl codex
Then attach caffeinate:
bash caffeinate -i -w <PID>
That is expected.
This command:
bash caffeinate -i
normally stays in the foreground without continuously printing status messages.
Verify it with:
bash pgrep -fl caffeinate
or:
bash pmset -g assertions
That stops caffeinate.
Your Codex process in another terminal is not necessarily affected, but macOS can resume its normal idle-sleep behavior.
Restart sleep prevention with:
bash caffeinate -i
If the caffeinate process terminated with the terminal, sleep prevention is no longer active.
Start it again, or use:
bash nohup caffeinate -i >/tmp/caffeinate.log 2>&1 &
caffeinate does not preserve the shell session itself.
If closing the terminal terminates Codex, use tmux for future long-running jobs:
bash tmux new -s codex
Do not treat caffeinate -i as a universal override for MacBook lid-close sleep.
For unattended Codex tasks, leave the MacBook open and let only the display sleep.
Usually, yes, provided the Mac itself remains awake.
With:
bash caffeinate -i
the display is still allowed to sleep while idle system sleep is prevented.
This is normally the preferred configuration.
Inspect them using:
bash ps -o pid,ppid,etime,command -p <PID>
If you still cannot confidently identify the right process, simply run:
bash caffeinate -i
instead of using -w.
If you started it with:
bash caffeinate -i -w 48231
it should exit when PID 48231 exits.
Check with:
bash pgrep -fl caffeinate
If the relevant entry is gone, that assertion is no longer active.
For a Codex task that is already running, the fastest solution is simply:
bash caffeinate -i
If you want sleep prevention to disappear automatically when that particular Codex process finishes, use:
bash pgrep -fl codex
followed by:
bash caffeinate -i -w <PID>
For future Codex sessions, start them directly with:
bash caffeinate -i codex
For especially long or unattended work, combine:
text Codex + caffeinate + tmux
This gives you a practical macOS workflow where the display can turn off, the computer remains awake, the terminal session can be detached, and Codex can continue working without an avoidable idle-sleep interruption.
More articles connected to the same themes, protocols, and tools.
Browse entries that are adjacent to the topics covered in this article.