Caffeinate AI: Keep macOS awake while one or more monitored apps are running (for long agentic sessions with ChatGPT/Codex or Claude) A developer released caffeinate-ai, a zsh script that keeps macOS from idle-sleeping while monitored applications such as ChatGPT and Codex are running, targeting long agentic coding sessions. The tool polls running apps every five seconds and wraps macOS's built-in caffeinate with -i so the system stays awake while the display can still sleep, and it ships with start, stop, restart, toggle, status, doctor, and run subcommands plus environment-variable configuration for interval, match mode, and target app list. | | /bin/zsh | | | | | | caffeinate-ai | | | | | | Keep macOS awake while one or more monitored applications are running. | | | | | | Setup: | | | 1. Add to local bin dir: ~/.local/bin/caffeinate-ai | | | 2. Make it executable: chmod +x ~/.local/bin/caffeinate-ai | | | 3. View help and usage: caffeinate-ai --help | | | | | | Defaults: | | | - Monitor ChatGPT/Codex TODO: add Claude | | | - Poll every 5 seconds | | | - Prevent idle system sleep with caffeinate -i | | | - Allow the display to sleep normally | | | - Produce no background logs unless --verbose is supplied | | | | | | Commands: | | | caffeinate-ai --help | | | caffeinate-ai start | | | caffeinate-ai stop | | | caffeinate-ai restart | | | caffeinate-ai toggle | | | caffeinate-ai status | | | caffeinate-ai doctor | | | caffeinate-ai run | | | caffeinate-ai | | | When called without a command, defaults to caffeinate-ai run , | | | which runs caffeinate-ai in the foreground of the current shell. | | | set -u | | | set -o pipefail | | | ----------------------------------------------------------------------------- | | | Constants | | | ----------------------------------------------------------------------------- | | | readonly SCRIPT PATH="${0:A}" | | | readonly SCRIPT NAME="${0:t}" | | | readonly STATE DIR="${HOME}/Library/Caches/caffeinate-ai" | | | readonly PID FILE="${STATE DIR}/caffeinate-ai.pid" | | | readonly LOCK DIR="${STATE DIR}/operation.lock" | | | readonly LOCK PID FILE="${LOCK DIR}/pid" | | | readonly LOG FILE="${STATE DIR}/caffeinate-ai.log" | | | ----------------------------------------------------------------------------- | | | Defaults / environment configuration | | | ----------------------------------------------------------------------------- | | | | | | These defaults can be overridden by environment variables: | | | | | | CAFFEINATE AI INTERVAL=10 | | | CAFFEINATE AI MATCH MODE=exact | | | CAFFEINATE AI APPS='ChatGPT:Codex:Visual Studio Code' | | | CAFFEINATE AI CAFFEINATE ARGS='-i' | | | | | | CLI arguments take precedence over environment configuration. | | | | | | POLL INTERVAL="${CAFFEINATE AI INTERVAL:-5}" | | | MATCH MODE="${CAFFEINATE AI MATCH MODE:-exact}" | | | VERBOSE=false | | | DRY RUN=false | | | | | | Add other applications to monitor here | | | | | | TARGET APPS= | | | "ChatGPT" | | | "Codex" | | | | | | CAFFEINATE ARGS= | | | "-i" | | | | | | if -n "${CAFFEINATE AI APPS:-}" ; then | | | TARGET APPS= | | | "${ @s/:/ CAFFEINATE AI APPS}" | | | | | | fi | | | if -n "${CAFFEINATE AI CAFFEINATE ARGS:-}" ; then | | | CAFFEINATE ARGS= | | | "${ @s/:/ CAFFEINATE AI CAFFEINATE ARGS}" | | | | | | fi | | | | | | PID of the caffeinate process owned by this watcher. | | | | | | CAFFEINATE PID="" | | | | | | Set to 1 only for a detached watcher launched by start . | | | | | | IS DAEMON="${CAFFEINATE AI DAEMON:-0}" | | | ----------------------------------------------------------------------------- | | | Help | | | ----------------------------------------------------------------------------- | | | usage { | | | cat <