{"slug": "use-caffeinate-to-keep-claude-code-and-codex-running-on-mac", "title": "Use caffeinate to keep Claude Code and Codex running on Mac", "summary": "MacOS ships with a built-in command-line tool called caffeinate that prevents the Mac from sleeping during long-running terminal tasks, making it especially useful for AI coding tools like Claude Code and Codex when connected to a VPN. By running caffeinate alongside these tools, users can avoid network disconnections and task interruptions caused by idle sleep.", "body_md": "# Use caffeinate to keep Claude Code and Codex running on Mac\n\nmacOS 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.\n\nAI coding tools are becoming part of my regular terminal workflow.\n\nTools 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:\n\nThe Mac goes to sleep.\n\nThis 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.\n\nEarlier, I had written about [No Sleep Page](https://www.narendravardi.com/sleep-page/), which is a simple webpage that prevents the laptop from sleeping. That works well when you want a browser-based solution.\n\nOne drawback is that No Sleep Page needs the browser tab to stay in the foreground. The page itself says:\n\nThis can only keep your computer awake if this tab is kept in the foreground.\n\nBut if you are already working inside the terminal, macOS has an even cleaner option.\n\nIt is called `caffeinate`\n\n.\n\n## What is `caffeinate`\n\n?\n\n`caffeinate`\n\nis a macOS command-line tool that prevents your Mac from sleeping.\n\nIt is already available on macOS, so there is nothing new to install.\n\nFor this workflow, you only need a few options:\n\n`-i`\n\n: prevent idle sleep. This is the one I use most often for terminal commands.`-d`\n\n: prevent the display from sleeping.`-t <seconds>`\n\n: keep the Mac awake for a fixed number of seconds.`-w <pid>`\n\n: keep the Mac awake until a specific process exits.\n\nYou can combine options. For example, `-di`\n\nmeans keep the display awake and prevent idle sleep.\n\nThe simplest version is:\n\n```\ncaffeinate\n```\n\nRun this in Terminal, and your Mac will stay awake until you stop the command using `Ctrl + C`\n\n.\n\nBut the more useful way is to run another command through `caffeinate`\n\n.\n\nFor example:\n\n```\ncaffeinate -i npm test\n```\n\nIn this case, macOS will stay awake while `npm test`\n\nis running. Once the command exits, `caffeinate`\n\nalso stops doing its work.\n\nThat is the main idea:\n\nKeep the Mac awake only while the important command is running.\n\nThe Terminal window does not need to stay in the foreground. You can start a long-running command with `caffeinate`\n\n, switch to another app, and macOS will still keep the machine awake while that command is active.\n\n## Using `caffeinate`\n\nwith Claude Code and Codex\n\nClaude 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.\n\nFor Claude Code:\n\n```\ncaffeinate -i claude\n```\n\nFor Codex:\n\n```\ncaffeinate -i codex\n```\n\nFor most cases, I would start with `-i`\n\n. The display can turn off, but the terminal work continues.\n\nIf you want to keep the display awake too, use `-di`\n\n:\n\n```\ncaffeinate -di claude\ncaffeinate -di codex\n```\n\n## Running it for a fixed time\n\nSometimes you do not want to attach `caffeinate`\n\nto a command. You just want the Mac to stay awake for some time.\n\nI 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.\n\n```\ncaffeinate -t 1800\n```\n\nThe time is in seconds. For one hour, use:\n\n```\ncaffeinate -t 3600\n```\n\n## Background or already-running tasks\n\nIf you are starting a background task now, you can background the whole `caffeinate`\n\ncommand:\n\n```\ncaffeinate -i ./long-running-task.sh &\n```\n\nIf the task is already running, find its process ID and use `-w`\n\n:\n\n```\ncaffeinate -i -w 12345\n```\n\nHere, `12345`\n\nis the process ID. `caffeinate`\n\nkeeps the Mac awake until that process exits.\n\n## Key takeaway\n\nIf you use Claude Code or Codex on macOS, `caffeinate`\n\nis one of those tiny commands worth knowing.\n\nYou do not need to install a new app.\n\nYou do not need to change system settings permanently.\n\nJust run the important command through `caffeinate`\n\n, and let the Mac stay awake until the work is done.\n\nReference:\n\n```\nman caffeinate\n```\n\n", "url": "https://wpnews.pro/news/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac", "canonical_source": "https://www.narendravardi.com/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac/", "published_at": "2026-07-17 14:38:19+00:00", "updated_at": "2026-07-17 14:51:11.989865+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["Claude Code", "Codex", "macOS", "Apple"], "alternates": {"html": "https://wpnews.pro/news/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac", "markdown": "https://wpnews.pro/news/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac.md", "text": "https://wpnews.pro/news/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac.txt", "jsonld": "https://wpnews.pro/news/use-caffeinate-to-keep-claude-code-and-codex-running-on-mac.jsonld"}}