caffeinate: keep your Mac awake from the terminal (every flag explained)

caffeinate is a small command that ships with every Mac: it keeps the Mac awake while something is running. Here's what each flag does, the three combinations worth remembering, and the one thing it can't stop.

The basics

Run a command through caffeinate, and the Mac stays awake until that command exits:

caffeinate -i ./long-job.sh

Run caffeinate on its own and it keeps the Mac awake until you press Ctrl-C. It works by holding a power assertion, a request macOS respects instead of going to sleep, and it releases it as soon as it exits. Nothing is left changed in your settings.

Every flag

FlagPreventsUse it when
-iIdle sleep of the systemA job needs the Mac to keep running while nobody touches it. The one you want most of the time.
-dThe display sleepingYou're watching output on screen, like a presentation or a dashboard.
-mThe disk idlingRarely needed on SSD Macs.
-sSystem sleep entirelyStronger than -i, but only works while the Mac is on power.
-uDeclares the user activeWakes the display and resets the idle timer, as if someone touched the keyboard. Without -t it lasts 5 seconds.
-t NSets a time limitKeep awake for N seconds, then stop.
-w PIDTies it to a processKeep awake until the process with that ID exits.

Three combinations worth remembering

Keep awake for one job

caffeinate -i npm run build

Done when the build is done. Add -s on a MacBook that's plugged in: caffeinate -is npm run build.

Keep awake for something already running

You started a job and then realised the Mac will sleep in ten minutes. Find its process ID and attach to it:

pgrep -f "npm run build"      # prints the process ID, say 48213
caffeinate -i -w 48213 &

Keep awake for a set time

caffeinate -i -t 7200 &     # two hours

The trailing & gives your prompt back while it runs.

What's keeping my Mac awake (or not)?

pmset -g assertions

This lists every active power assertion and the process holding it: your caffeinate, a video call, a download. If your Mac won't sleep when it should, this is where to look. pmset -g on its own shows your current sleep settings.

What caffeinate can't do

The settings alternative

If you'd rather the Mac never sleeps on power, change the setting instead:

The display still turns off to save power; the work carries on.

Awake is only half of it

An awake Mac running a long job still needs you when the job asks a question or fails. If you've stepped away, the other half is being able to see and answer it. PandaSend Terminal runs your sessions on the Mac and lets you open the same live session from your phone, and it sends a notification when one is waiting for input. For the full routine, see how to keep builds and coding agents running when you leave your Mac, and how to get notified when a command finishes.