How to Use the caffeinate Terminal Command on Mac

Published 18 July 2026 · Agata Raap

Here is the whole answer, because you came here for a command, not a story:

caffeinate            # stays awake until you press Control-C
caffeinate -d         # also keeps the display on
caffeinate -t 3600    # stays awake for one hour, then releases

caffeinate is a command built into every Mac that stops the machine from going to sleep. Run it in Terminal and your Mac stays awake until you stop it. Add flags to keep the display on, set a timer, or hold the Mac awake while another command runs. That’s the entire idea. The rest of this is the flags, the honest caveats, and the point where I stopped typing it by hand and wrote an app instead.

What caffeinate actually is

caffeinate is a tiny built-in macOS utility. It lives at /usr/bin/caffeinate, it’s been in macOS since Mountain Lion, and there is nothing to install — no Homebrew, no download, no sudo. If you’re on a Mac, it’s already there. You can prove it to yourself:

man caffeinate        # the built-in manual, which is the real source of truth

Under the hood it does one unglamorous thing: it creates a power assertion and holds it open. A power assertion is macOS’s own mechanism for “something needs the machine to stay awake right now.” Backups use them, video playback uses them, and caffeinate just lets you create one from the command line. When the command exits, the assertion is released and your Mac goes back to its normal sleep schedule. Nothing is changed permanently — this is the reassuring part. You are not editing power settings; you’re holding a button down, and letting go puts everything back.

There are a few different kinds of sleep it can block — idle sleep, display sleep, disk sleep, system sleep — which is what the flags are for.

The basic command

Open Terminal (it’s in Applications → Utilities, or hit ⌘-Space and type “Terminal”) and run:

caffeinate

The cursor just sits there. That’s it working — the terminal is “busy” running caffeinate, and your Mac won’t idle-sleep as long as it is. To stop it, press Control-C in that same window. Sleep goes back to normal immediately.

If you want your prompt back but still want the Mac awake, send it to the background with &:

caffeinate &          # runs in the background, hands you your prompt back

Now caffeinate is detached and you can keep working. To stop a backgrounded one you can’t just Control-C — run:

killall caffeinate    # releases every caffeinate assertion at once

That’s the whole beginner path. Everything below is refinement.

Flag reference

These are the flags worth knowing. Definitions are the ones from man caffeinate — I’ve kept them exact because this is precisely where half the blog posts about caffeinate quietly get it wrong.

Flag What it does
-i Prevent idle system sleep. This is the default assertion when you pass no flags.
-d Prevent the display from sleeping.
-m Prevent the disk from idle-sleeping.
-s Prevent the system from sleeping. Only effective on AC power.
-u Declare the user is active — wakes the display. Defaults to a ~5-second assertion unless paired with -t.
-t <sec> Set a timeout in seconds, after which the assertion is released automatically.
-w <pid> Hold the assertion until the process with the given PID exits.
caffeinate <cmd> Run a command and stay awake until that command finishes, then release automatically.

You can stack them. caffeinate -dimsu blocks every kind of sleep at once — idle, display, disk, system, and declares the user active. It’s the “just stay awake, I mean it” sledgehammer. Most of the time you want far less than that.

Set a timer

-t takes seconds, which is mildly annoying because nobody thinks in seconds. The conversions you’ll actually use:

caffeinate -t 1200    # 20 minutes
caffeinate -t 1800    # 30 minutes
caffeinate -t 3600    # 1 hour
caffeinate -t 7200    # 2 hours

After the timeout, caffeinate exits on its own and your Mac is free to sleep again — no cleanup, no lingering process. This is the flag I reach for most, because “stay awake for exactly this long and then stop caring” is the honest shape of most real situations. Combine it with & and you get a fire-and-forget: caffeinate -t 3600 & keeps the Mac up for an hour in the background while you get on with things.

Keep the Mac awake while a command runs

This is the one that makes caffeinate genuinely useful rather than just a fancy off-switch. Hand it a command and it stays awake for exactly as long as that command takes, then releases:

caffeinate -i npm run build       # awake only while the build runs
caffeinate -i make                # same, for a long compile
caffeinate -i rsync -a src/ dst/  # awake for the duration of the copy

The moment npm run build finishes — success or failure — the assertion drops. No timer to guess, no killall to remember. You’re tying “stay awake” to a real event instead of a wall-clock estimate, which is almost always what you actually meant.

If the long thing is already running and you forgot to wrap it, you’re not stuck. Find its PID and wait on it:

pgrep -f "npm run build"          # prints the process id, say 4821
caffeinate -w 4821                # awake until process 4821 exits

Keep the display on

By default caffeinate keeps the machine awake but lets the screen sleep. For a dashboard on a wall, a status board, a long video, or a presentation where you don’t want to touch anything, add -d:

caffeinate -d                     # screen stays on until Control-C
caffeinate -d -t 3600             # screen on for an hour, then normal

Without -d your display will dim and sleep on its usual schedule even while caffeinate runs — the Mac underneath just won’t fully sleep. That surprises people, so: -d is the difference between “the computer is awake” and “the screen is on.”

The lid-closed reality check

Here’s the caveat everyone hand-waves past, so I’ll be blunt about it: caffeinate does not keep a MacBook awake with the lid closed on battery. macOS forces clamshell sleep the moment you shut the lid, and a power assertion does not override that. You can caffeinate -dimsu all you like — close the lid on battery and it sleeps anyway.

What does keep a Mac running lid-closed is the old clamshell recipe: on AC power, with an external display connected. That’s a hardware/OS rule, not something a command talks its way around. If you specifically want lid-closed runs, the clamshell guide over on perked walks through the conditions and the workarounds honestly — it’s the same corner I kept banging my shin on.

A handy alias

If you find yourself typing this a lot, give it a name. Add one line to ~/.zshrc:

alias stayawake='caffeinate -d'

Then source ~/.zshrc (or open a new terminal) and stayawake keeps your Mac and screen up until Control-C. Small, but it’s the difference between a command you remember and one you look up every time.

Where caffeinate stops being enough

caffeinate is a great answer to “keep this Mac awake for the next while.” It’s a worse answer to a few things people keep hoping it solves:

  • It won’t fake your presence. No mouse jiggle, no keypress simulation — so it won’t hold your Slack or Teams “active” dot. It keeps the machine awake, not your status.
  • It won’t beat lid-closed sleep on battery, as above.
  • It’s a babysitting job for long runs. This is the one that got me. I run AI coding agents — Claude Code, Cursor — that chew through work for twenty, forty, ninety minutes at a stretch. caffeinate -w <pid> technically covers it, but in practice you’re finding the PID, remembering to run it, forgetting, coming back to a Mac that slept mid-task and an agent that stalled. Doing that dance at 2am, one too many times, is why I stopped.

So I built perked. It’s a native menu-bar app that watches for coding agents — Claude Code, Cursor, Aider, Codex, Gemini, Copilot — and keeps the Mac awake automatically while they’re working, then lets it sleep when they stop. It’s power-aware (only holds on AC or above a battery threshold you set), it handles clamshell, and there’s no caffeinate to type or pid to hunt. If your reason for keeping the Mac awake is “my agents are running overnight,” the overnight-runs guide is the exact scenario it was built for, and there’s a plain-English caffeinate walkthrough too if you’d rather stay on the command line — I’m not precious about it.

And if you’d sooner have a general-purpose GUI toggle than either the command or an agent-specific app, that’s completely reasonable — Amphetamine, KeepingYouAwake, Caffeine, Lungo, and Theine all do the “click to stay awake” job well. I keep an honest side-by-side of the alternatives so you can pick the one that fits, even when the answer isn’t mine. There’s also the full guides index if you want to poke around.

For most people, most of the time, the answer really is just caffeinate -d -t 3600 and getting on with your day. Reach for more only when caffeinate makes you babysit it.

Keeping a Mac awake for something weirder than a download? Tell me what — I’m on X and Bluesky, kettle’s on.

Frequently asked questions

How do I keep my Mac awake from Terminal?
Open Terminal and run caffeinate. Your Mac stays awake until you press Control-C in that window. Add -d to also keep the display on, or -t 3600 to auto-release after an hour.
Is caffeinate installed on every Mac by default?
Yes. caffeinate ships with macOS — it lives at /usr/bin/caffeinate and has since OS X Mountain Lion. There is nothing to install, no Homebrew, no download. Type man caffeinate to read the built-in manual.
How do I stop caffeinate once it's running?
If it's running in the foreground, press Control-C. If you sent it to the background with & or a timer, run killall caffeinate to release every assertion at once. Closing the Terminal window also stops a foreground caffeinate.
What's the difference between the -d and -i flags?
-i prevents the system from going to idle sleep (the default behaviour when you run caffeinate with no flags). -d additionally keeps the display awake — use it when you want the screen to stay on for a dashboard, video, or presentation, not just the machine running underneath.
Can caffeinate keep my MacBook awake with the lid closed?
Not on its own. On battery, macOS forces clamshell (lid-closed) sleep and caffeinate does not override it. You need AC power plus an external display, or a tool that holds the assertion through clamshell. This is the caveat competitors most often get wrong.
Does caffeinate keep me active in Slack or Teams?
No. caffeinate blocks system and display sleep, but it does not simulate mouse or keyboard input, so it won't hold your green 'active' dot in Slack, Teams, or similar. It keeps the Mac awake, not your presence indicator.
Does caffeinate drain the battery?
caffeinate itself is nearly free — it's a tiny process that just holds a power assertion. What drains the battery is the sleep it prevents: a Mac that stays awake and keeps its display on uses more power than one allowed to nap. Use -t to cap how long it runs, and skip -d if you don't need the screen.