caffeinate on Mac: the complete guide
caffeinate -dimsu keeps your Mac fully awake until you press Control-C. Add a command to tie it to a task (caffeinate -dimsu npm run build), or -t 3600 to hold for a set number of seconds. It's built into macOS, needs no install, and undoes itself when it exits.
Don't want to read all of this? Use the free caffeinate command builder. Pick what you're doing and it writes the exact command for you.
What caffeinate actually is
Every Mac already ships with it — there's nothing to download. When you run caffeinate, macOS creates a power assertion: a formal request that the system not sleep. It's the same underlying mechanism apps use when they say "don't sleep, I'm playing a video."
The important property is that assertions are tied to the process. When caffeinate exits, the assertion disappears and your normal sleep settings return instantly. You are never left with a Mac that won't sleep because you forgot to undo a setting — which is exactly the risk when people go poking at pmset instead.
Every flag, explained
Flags combine, which is why you see -dimsu so often — it's simply all of the main ones at once, the "keep everything awake" setting.
The commands you'll actually use
Keep the Mac awake until you stop it
caffeinate -dimsu
Runs until you press Control-C. Simple, and the most common use.
Keep it awake for exactly one hour
caffeinate -dimsu -t 3600
Time is in seconds: 1800 = 30 min, 3600 = 1 hour, 18000 = 5 hours.
Keep it awake only while a command runs
caffeinate -dimsu npm run build
caffeinate -dimsu claude
caffeinate -dimsu ./long-script.sh
This is the best pattern for scripts and long jobs. The Mac stays awake for exactly as long as the job takes, then sleep behaviour returns on its own. Nothing to remember afterwards.
Keep it awake for a process that's already running
pgrep -f "ollama" # find the PID
caffeinate -dimsu -w 4821
Useful when you started something and only then realised the Mac might sleep on you.
Let the screen sleep but keep the machine working
caffeinate -ims
Drop the -d. The display can turn off (saving power) while the system keeps running. Good for downloads and headless jobs.
How to check that it's actually working
Don't assume — verify. This lists every active power assertion on the system, with the process holding each one:
pmset -g assertions
Look for PreventUserIdleSystemSleep and PreventUserIdleDisplaySleep set to 1, with caffeinate named as the owner. If they're not there, your command isn't doing what you think it is.
Stopping it
pkill caffeinate
Or just press Control-C, or close the terminal tab. All three work.
The three things caffeinate can't do
It's a genuinely good tool and for many people it's all they need. But it has real limits, and it's better to know them up front than to discover them at 3am with a dead job.
- It can't survive a closed lid. Clamshell sleep isn't something a normal assertion overrides, particularly on battery. If you need lid-shut running, caffeinate is not the answer.
- It has no idea about your battery. It will happily hold your Mac awake until the battery is flat. There is no low-power cutoff.
- It won't stop if things get hot. No thermal awareness at all. Fine on a desk; less fine for an unattended overnight run.
And the practical one: you have to remember to type it, before the job starts, every single time. Miss it once on the run that mattered and you're back where you started.
Handy shortcut
If you use it constantly, alias it in ~/.zshrc:
alias stayawake="caffeinate -dimsu"
alias awake1h="caffeinate -dimsu -t 3600"
Then source ~/.zshrc and just type stayawake.
Or skip the terminal entirely
Awake is a menu-bar switch that does all of the above in one click — plus the parts caffeinate can't: lid-closed running on power, an automatic stop at 20% battery, and a cutoff if your Mac starts running hot. Free 3-day trial, no card.
Download Awake for MacGenuinely: if caffeinate -dimsu covers your needs, use it. It's free and it works. Awake is for when you want it automatic and safe to leave unattended.
Common questions
Do I need to install caffeinate?
No. It's built into every version of macOS. Open Terminal and it's there.
Does caffeinate work with the lid closed?
No — closing the lid triggers a clamshell sleep that a standard assertion can't override, especially on battery. That needs a clamshell-capable app, and should only be done on power or with thermal safeguards.
Will it drain my battery?
It can. caffeinate has no low-battery cutoff — it holds until you stop it. Use -t to bound it, or a tool with a battery guard.
What's the difference between -i and -s?
-i prevents idle sleep (the timer-based kind). -s prevents system sleep more broadly but only applies on AC power. Using both is the safe default.
Can I make it permanent?
You can with pmset, but don't — it changes system settings that stay changed until you undo them, and it's easy to forget. caffeinate's whole advantage is that it cleans up after itself.
Related: keep your Mac awake for Claude Code · running with the lid closed · comparisons