Use caffeinate to keep Claude Code and Codex running on Mac
macOS ships with a small command called `caffeinate` that keeps your Mac awake during long-running terminal tasks. It is especially useful for Claude Code and Codex sessions over office VPN, where sleep can disconnect the network session and interrupt the work.
AI coding tools are becoming part of my regular terminal workflow.
Tools like Claude Code and Codex are useful when I want to explore a codebase, ask for a refactor, run tests, or let an agent work through a task. But there is one very boring problem that can interrupt this flow:
The Mac goes to sleep.
This becomes more painful when I am connected to an office VPN. Slightly longer-running tasks need the network connection to stay alive. If the laptop sleeps, the VPN can disconnect, and the task may pause, fail, or need manual attention again.
Earlier, I had written about No Sleep Page, which is a simple webpage that prevents the laptop from sleeping. That works well when you want a browser-based solution.
One drawback is that No Sleep Page needs the browser tab to stay in the foreground. The page itself says:
This can only keep your computer awake if this tab is kept in the foreground.
But if you are already working inside the terminal, macOS has an even cleaner option.
It is called caffeinate.
What is caffeinate?
caffeinate is a macOS command-line tool that prevents your Mac from sleeping.
It is already available on macOS, so there is nothing new to install.
For this workflow, you only need a few options:
-i: prevent idle sleep. This is the one I use most often for terminal commands.-d: prevent the display from sleeping.-t <seconds>: keep the Mac awake for a fixed number of seconds.-w <pid>: keep the Mac awake until a specific process exits.
You can combine options. For example, -di means keep the display awake and prevent idle sleep.
The simplest version is:
caffeinate
Run this in Terminal, and your Mac will stay awake until you stop the command using Ctrl + C.
But the more useful way is to run another command through caffeinate.
For example:
caffeinate -i npm test
In this case, macOS will stay awake while npm test is running. Once the command exits, caffeinate also stops doing its work.
That is the main idea:
Keep the Mac awake only while the important command is running.
The Terminal window does not need to stay in the foreground. You can start a long-running command with caffeinate, switch to another app, and macOS will still keep the machine awake while that command is active.
Using caffeinate with Claude Code and Codex
Claude Code and Codex can take time when the task is non-trivial: reading a large repository, running tests, fixing lint errors, refactoring files, or reviewing a pull request. These are exactly the runs where I do not want idle sleep to interrupt the session.
For Claude Code:
caffeinate -i claude
For Codex:
caffeinate -i codex
For most cases, I would start with -i. The display can turn off, but the terminal work continues.
If you want to keep the display awake too, use -di:
caffeinate -di claude
caffeinate -di codex
Running it for a fixed time
Sometimes you do not want to attach caffeinate to a command. You just want the Mac to stay awake for some time.
I frequently use it for 30 minutes after a plan is generated. That gives the agent a focused window for implementation and execution without the Mac sleeping in the middle.
caffeinate -t 1800
The time is in seconds. For one hour, use:
caffeinate -t 3600
Background or already-running tasks
If you are starting a background task now, you can background the whole caffeinate command:
caffeinate -i ./long-running-task.sh &
If the task is already running, find its process ID and use -w:
caffeinate -i -w 12345
Here, 12345 is the process ID. caffeinate keeps the Mac awake until that process exits.
Key takeaway
If you use Claude Code or Codex on macOS, caffeinate is one of those tiny commands worth knowing.
You do not need to install a new app.
You do not need to change system settings permanently.
Just run the important command through caffeinate, and let the Mac stay awake until the work is done.
Reference:
man caffeinate