Caffeinate on Mac: keep it awake while an agent runs
Pick macOS or Linux, how long, power and lid. You get the exact command, how to check it's working, and a list of what it won't cover.
caffeinate ships with macOS and stops the Mac sleeping while a job runs. The -i flag stops idle sleep, -s stops system sleep (on AC power only), and -t sets a timeout in seconds. Apple describes closed-lid use with an external display, power connected and an external keyboard and mouse or trackpad. caffeinate's manual makes no promise about the lid.
Answer four questions and the builder prints the command for macOS or Linux. The command you type stays in your browser.
What you type stays in your browser. A share link carries only your answers, and claude or codex.
Keeps this Mac awake while the lid stays open
Idle sleep and system sleep are both held off while the Mac is plugged in.
Your command
caffeinate -is claude
-i stops idle sleep. -s stops system sleep, and only applies while the Mac is on AC power.
Wrapping the command ties the keep-awake to it: caffeinate releases it when the command exits.
Check it is working
pmset -g assertions
Look for "caffeinate command-line tool" under PreventUserIdleSystemSleep, and under PreventSystemSleep when you're on power.
pmset, if you want it to last
pmset -g custom
sudo pmset -c sleep 0
Only if you want it to last. The first line shows your current settings, so note the sleep value. The second turns the plugged-in sleep timer off until you set it back. A sleep timer of 0 doesn't stop a closed lid from sleeping the Mac, and a forgotten setting drains the battery. caffeinate is usually the cleaner choice.
What this will not cover
A closed lid. If you shut it, expect the Mac to sleep. Apple describes closed-lid use with an external display, power connected and an external keyboard and mouse or trackpad.
Unplugging.-s stops applying when the Mac runs on battery, and -i still holds off idle sleep.
A reboot or an OS update that restarts the machine. Every run ends when it restarts.
A network change or drop, such as a new wifi network, a VPN reconnecting or tethering. A call in flight can fail.
Closing the terminal window. The agent and the keep-awake command stop with it, unless they run inside tmux.
The agent crashing or hitting a usage limit. This keeps the machine awake. It doesn't restart anything.
Run the agent and the keep-awake inside tmux so a closed terminal window does not end them. The tmux cheat sheet has the commands.
Commands last checked September 2026 against the caffeinate, pmset, systemd-inhibit and logind.conf manuals and Apple Support. The method section below links each source.
On a Mac the builder uses caffeinate, which comes with macOS. The -i flag creates an assertion that stops idle sleep. The -s flag creates one that stops system sleep, and the manual says it's valid only while the Mac runs on AC power, so the builder adds it only when you say you're plugged in. Sources: caffeinate(8) manual.
When caffeinate wraps a command, it holds its assertions for as long as the command runs. The manual says the -t timeout isn't used in that form. So for a fixed number of hours, the builder gives you a separate caffeinate -t command for its own terminal tab, with the hours converted to seconds. Sources: caffeinate(8) manual.
pmset is how you check, and how you make a setting stick. pmset -g assertions lists the assertions caffeinate takes. pmset -g custom shows your current settings, and setting sleep to 0 turns the system sleep timer off until you change it back. It's a saved setting, and it won't fix a closed lid. Sources: pmset(1) manual.
On Linux the builder uses systemd-inhibit. Its --what option takes a colon-separated list. That list includes sleep, which blocks suspend and hibernate, and handle-lid-switch, which blocks the lid switch handling. The lock is held while the command it runs is alive, and --list shows the active locks. The builder adds handle-lid-switch only when you say the lid will be closed. For a permanent change, the HandleLidSwitch setting in logind.conf controls what a closed lid does. Sources: systemd-inhibit(1) manual, logind.conf(5) manual.
How this was tested: the builder only prints commands and runs nothing on your machine. We checked the caffeinate timeout and the assertion names by running caffeinate -is-t on a MacBook on AC power with macOS 26.6.2 on 2026-09-30, then reading the assertions back with pmset -g assertions. We built the Linux commands from the manual and haven't run them for this page.
Last checked September 2026.
Worked examples
Claude Code on a plugged-in MacBook, lid open
Inputs: macOS, until the command exits, plugged in, lid open, claude.
caffeinate -is claude
Idle sleep and system sleep are both held off while claude runs and the Mac is on power. The lid has to stay open. Shutting it would put the Mac to sleep.
An 8 hour Codex run on battery
Inputs: macOS, for 8 hours, on battery, lid open, codex.
caffeinate -i -t 28800
A caffeinate running in one terminal tab holds off idle sleep for 28,800 seconds, and Codex starts in another. -s is left out because it needs AC power. The battery keeps draining.
A Linux laptop with the lid closed
Inputs: Linux, until the command exits, plugged in, lid closed, claude.
systemd-inhibit --what=sleep:handle-lid-switch --why="agent run" claude
While claude runs, suspend and the lid-close action are blocked. If your desktop handles the lid itself, test with the lid closed for a minute before you rely on it.
Cite this page
Hivra, "Keep Mac Awake Command Builder", https://hivra.cloud/tools/keep-mac-awake, facts last checked September 2026.
Source: <a href="https://hivra.cloud/tools/keep-mac-awake">Hivra</a>, Keep Mac Awake Command Builder, facts last checked September 2026.
Common questions
How do I use caffeinate on Mac?+
Open Terminal and run caffeinate -i. That stops idle sleep until you press Ctrl-C. To keep the Mac awake only while a command runs, put caffeinate in front of it, for example caffeinate -is claude. The -s flag also blocks system sleep, but only on AC power. The -t flag sets a timeout in seconds, though caffeinate ignores it when it wraps a command.
What is the difference between caffeinate -i and -s?+
-i creates an assertion that stops the system from idle sleeping. -s creates one that stops the system from sleeping, and Apple's manual says it's valid only while the Mac runs on AC power. On battery, only -i applies.
How do I keep my Mac awake with the lid closed?+
caffeinate doesn't promise that. Apple documents closed-lid use of a MacBook when an external display is connected, with power connected and an external keyboard and mouse or trackpad. Without those, plan on the lid sleeping the Mac. If a run has to outlast a closed laptop, run it on a machine that stays on.
Will Claude Code keep running if my computer goes to sleep?+
No. Sleep suspends every process on the machine, Claude Code included, and a call in flight can fail when the machine wakes. Keep the Mac awake with caffeinate while it works, or run it on a machine that stays on.
How do I stop caffeinate?+
Press Ctrl-C in the terminal tab where it runs. If it wraps a command, it stops when the command exits, and a timed caffeinate -t stops when the timeout ends. To stop one you started in the background, run pkill caffeinate.
Does caffeinate work on battery?+
Partly. -i works on battery and stops idle sleep, though the battery keeps draining. -s only applies on AC power, so the builder leaves it out when you say you're on battery.
How do I keep a Linux laptop awake when I close the lid?+
Run the command under systemd-inhibit --what=sleep:handle-lid-switch. The lock lasts while the command runs. For a permanent change, set HandleLidSwitch=ignore in logind.conf. Some desktops handle the lid themselves, so test with the lid closed for a minute.
Put your agent on a computer that stays on.
From $9.99/mo for 2 vCPU and 4 GB, not paused for inactivity. Your own Claude or ChatGPT login, or your own AI key. 7-day money-back guarantee on card payments.