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
| Flag | Prevents | Use it when |
|---|---|---|
-i | Idle sleep of the system | A job needs the Mac to keep running while nobody touches it. The one you want most of the time. |
-d | The display sleeping | You're watching output on screen, like a presentation or a dashboard. |
-m | The disk idling | Rarely needed on SSD Macs. |
-s | System sleep entirely | Stronger than -i, but only works while the Mac is on power. |
-u | Declares the user active | Wakes the display and resets the idle timer, as if someone touched the keyboard. Without -t it lasts 5 seconds. |
-t N | Sets a time limit | Keep awake for N seconds, then stop. |
-w PID | Ties it to a process | Keep 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
- Closing the lid still sleeps a MacBook. The supported exception is closed-display mode: on power, with an external display, keyboard and mouse. There are settings and third-party tools that override lid sleep, with real risk of overheating in a bag. The simple rule: leave the lid open and the charger in.
- It doesn't survive the terminal closing if it's attached to that terminal. Start it in the background with
&, or attach it to the job with-w, or use the setting below. - It doesn't keep the network awake by itself if the Mac does sleep. Remote connections to a sleeping Mac fail, whatever tool you use.
The settings alternative
If you'd rather the Mac never sleeps on power, change the setting instead:
- MacBook: System Settings → Battery → Options → Prevent automatic sleeping on power adapter when the display is off.
- Desktop Mac: System Settings → Energy → Prevent automatic sleeping when the display is off.
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.