caffeinate on Mac: every flag explained (and why it can't keep the lid closed)

The built-in Terminal command that keeps a Mac awake — what each flag really does, with copy-paste examples.

Updated · by Sasha Yakovlev, maker of Lidwork

Quick answer

caffeinate is a built-in macOS command that stops your Mac from sleeping while it runs. caffeinate -i blocks idle system sleep, -d keeps the display on, -t 3600 stops after an hour, and caffeinate -i your-command lasts exactly as long as the command. It cannot keep a MacBook awake with the lid closed and no external display.

People search for “caffeine Mac” because of Caffeine, an old menu-bar app with a coffee-cup icon. Today the tool that does the job is already on your Mac: caffeinate, a command-line utility Apple ships with macOS. No download, no password, and it reverts the moment it stops.

The one-line version

caffeinate -i

Run that in Terminal and your Mac won't idle-sleep until you press Control-C. That's 80% of what anyone needs. The rest of this page explains the flags so you can pick the right one.

Every flag, explained

caffeinate works by creating power assertions: requests to macOS that say “don't do this kind of sleep while I'm alive”. Each flag asks for a different one. You can combine them, e.g. caffeinate -dims.

FlagWhat it preventsWhen to use it
-iIdle system sleep. The display may still turn off.Downloads, builds, scripts. The default choice.
-dDisplay sleep — the screen stays on.Presentations, dashboards, watching a long job.
-mDisk idle sleep.Rarely needed on SSD Macs; harmless.
-sSystem sleep — only while on AC power.Plugged-in machines that must stay up.
-uDeclares the user active: wakes the display and resets idle timers. Without -t it lasts 5 seconds.Waking a remote Mac's screen; nudging idle timers.
-t NReleases the assertion after N seconds.“Stay awake for an hour”: -t 3600.
-w PIDHolds the assertion until process PID exits.Attaching to something already running.

With no flags at all, caffeinate behaves like -i.

Commands worth remembering

# awake until Control-C
caffeinate -i

# awake for 90 minutes, then normal
caffeinate -i -t 5400

# awake exactly as long as a command runs
caffeinate -i ./long-script.sh
caffeinate -i rsync -a ~/Movies/ /Volumes/Backup/Movies/

# keep the screen on too (presentations)
caffeinate -di

# attach to a process that's already running
caffeinate -i -w $(pgrep -n ffmpeg)

The command form (caffeinate -i some-command) is the most underrated: the Mac sleeps normally again as soon as the job finishes, so you never leave it awake by accident. Our caffeinate command builder assembles these with checkboxes.

caffeinate in scripts and aliases

Because it wraps any command, caffeinate is easy to bake into your own tooling:

# ~/.zshrc — "awake" prefix for long jobs
alias awake='caffeinate -i'
# usage: awake make release

# inside a script: keep the Mac up for this script's lifetime
caffeinate -i -w $$ &

The last line attaches caffeinate to the script's own process ID ($$), so the Mac stays awake until the script exits — however it exits.

How to stop it

  • Press Control-C in the Terminal window.
  • Close the Terminal window or tab.
  • From anywhere: killall caffeinate.

Because an assertion belongs to a process, it disappears when the process does — including if Terminal crashes. There is nothing to clean up afterwards, which is what makes caffeinate so safe.

Check that it's working

pmset -g assertions | grep -i caffeinate

You'll see lines like PreventUserIdleSystemSleep owned by caffeinate. The full pmset -g assertions output also shows which other apps are keeping your Mac awake — handy when it won't sleep and you don't know why.

Why caffeinate can't keep the lid closed

This is the question behind half the searches. Closing a MacBook's lid is not “idle”: it's an explicit sleep request, and on a MacBook without an external display macOS sleeps even while idle-sleep assertions are held. So with caffeinate -i running, you shut the lid, and your build pauses.

There are three ways around it:

  1. Clamshell mode: plug in power, an external display and a keyboard/mouse. Apple supports this setup officially. More on clamshell mode.
  2. sudo pmset -a disablesleep 1: disables sleep globally, lid included. It's persistent: you must run sudo pmset -a disablesleep 0 afterwards, or the Mac stays awake in your bag.
  3. An app that manages that switch and turns it back off for you. That's what Lidwork does — including when the app itself crashes.

caffeinate vs menu-bar apps

Apps like KeepingYouAwake are friendly front-ends for caffeinate: same assertions, a menu-bar icon instead of a Terminal window, often with a battery cutoff. If you only ever need the lid open, they're a fine free choice. If you need the lid closed, look at what an app does to the disablesleep switch and what happens after a crash — Lidwork vs caffeinate lays that out side by side.

FAQ

Does caffeinate work with the lid closed?

Not on its own. caffeinate holds power assertions against idle sleep; closing a MacBook's lid without an external display triggers sleep anyway. For lid-closed use you need sudo pmset -a disablesleep 1 (and to undo it later) or an app that manages that switch.

How do I stop caffeinate?

Press Control-C in the Terminal window where it runs, close that window, or run killall caffeinate. Normal sleep returns immediately.

Is caffeinate safe?

Yes. It changes no settings and needs no password; the assertion ends the moment the process exits, even if it crashes.

What is the difference between caffeinate -i and -s?

-i prevents idle sleep on battery and on power. -s prevents system sleep but only while the Mac is on AC power.