caffeinate Command on Mac: Every Flag With Examples
How to use caffeinate on Mac: what -d, -i, -m, -s, -u, -t and -w do, copy-ready examples, how to check it with pmset, and why your Mac may still sleep.
GuideUpdated 8 min read
On this page
To keep a Mac awake from Terminal, run caffeinate. It stops idle sleep until you press ⌃C (Control-C). Add -d to keep the screen on too, add -t 3600 to stop after an hour, or put a command after it (caffeinate -i make) to stay awake only while that command runs.
What the caffeinate command does on a Mac
caffeinate is built into macOS at /usr/bin/caffeinate. It creates a power assertion, a request that tells macOS not to sleep for a reason right now. It doesn't change your sleep settings. When caffeinate exits, the assertion goes away and your Mac sleeps on its normal schedule again, so there's nothing to undo.
Before you start:
- It doesn't need
sudoor an admin password. - With no flags, it prevents idle system sleep only. The display can still turn off, which often makes it look like the Mac went to sleep when it didn't.
- The common flags only prevent idle sleep. They don't stop sleep you trigger yourself, and you shouldn't rely on them to keep a MacBook awake with the lid closed (more on that below).
The full reference is the manual page. Run man caffeinate in Terminal to read it.
Keep your Mac awake until you press Control-C
Open Terminal (in Applications ▸ Utilities, or search for it with Spotlight).
Run one of these:
bash caffeinate -d-dkeeps the display on, which also keeps the Mac awake. Use plaincaffeinate(orcaffeinate -i) if the screen may turn off but the Mac should keep working, for example during a download.Leave the Terminal window open. The prompt doesn't come back while
caffeinateruns.Press ⌃C in that window when you're done.
Run it in the background instead
To get your prompt back, add &:
caffeinate -i &This starts caffeinate as a background job in the current shell. To stop it, run kill %1 in the same window (if it's your only background job there), or end every running copy:
killall caffeinateThis ends all caffeinate processes, including any that a script started. Closing the Terminal window normally ends a background caffeinate started from it as well.
What -d, -i, -m, -s and -u mean
You can combine flags in one command, for example caffeinate -di.
| Flag | Assertion it creates | What it does | Watch out for |
|---|---|---|---|
-d | PreventUserIdleDisplaySleep | Keeps the display from sleeping (and so keeps the Mac awake) | Uses more power, since the screen stays on |
-i | PreventUserIdleSystemSleep | Keeps the Mac from idle sleeping; the display may still sleep | Used when you give no assertion flag |
-m | PreventDiskIdle | Keeps the disk from idle sleeping | Doesn't stop the system or display from sleeping |
-s | PreventSystemSleep | Keeps the system from sleeping | Only valid on AC power; has no effect on battery |
-u | UserIsActive | Declares the user active: turns the display on if it's off and keeps it from idle sleeping | Lasts 5 seconds unless you add -t |
-t <seconds> | (timer) | Releases the assertion after that many seconds | Ignored when you run a command with caffeinate |
-w <pid> | (watch) | Releases the assertion when that process exits | Ignored when you run a command with caffeinate |
Copy-ready versions:
caffeinate -d # keep the screen on until Control-C
caffeinate -i # keep the Mac awake, let the screen turn off
caffeinate -m # keep the disk from idle sleeping
caffeinate -s # keep the system awake while on AC power
caffeinate -u -t 1 # wake the display now, as if you touched the keyboard
caffeinate -di # keep both the Mac and the screen awakeUse -i for long jobs where nobody watches the screen (downloads, backups, builds). Use -d for presentations, dashboards or anything you need to see.
Keep it awake for a set time with -t
-t takes seconds, not minutes:
caffeinate -d -t 3600This keeps the screen on for one hour, then lets the Mac sleep as usual. Without -d or another assertion flag, -t uses the default idle-sleep assertion.
| You want | Seconds | Command |
|---|---|---|
| 30 minutes | 1800 | caffeinate -t 1800 |
| 1 hour | 3600 | caffeinate -t 3600 |
| 2 hours | 7200 | caffeinate -t 7200 |
| 8 hours | 28800 | caffeinate -t 28800 |
If you'd rather think in minutes, let the shell do the arithmetic. This works in zsh, the default shell on current macOS:
caffeinate -i -t $((90 * 60))That's 90 minutes. Add & at the end to get the prompt back while the timer runs.
Stay awake while a command or process runs
Wrap a command
Put the command after caffeinate. The assertion lasts exactly as long as the command:
caffeinate -i make
caffeinate -i ./render.sh
caffeinate -i rsync -a ~/Projects/ /Volumes/Backup/Projects/When the command finishes, caffeinate exits and the Mac can sleep again. You don't need -t here, and it's ignored with a command anyway.
Watch a process that's already running (-w)
If the job is already going, give caffeinate its process ID (PID):
Find the PID. Activity Monitor shows it in the PID column, or run this in Terminal:
bash pgrep -x ffmpegThis prints the PID of every process named exactly
ffmpeg.Start
caffeinatewith that PID:bash caffeinate -i -w 4321Replace
4321with the real number. The assertion ends when that process exits.
Two useful variations:
caffeinate -i -w $(pgrep -n ffmpeg) # watch the newest process matching ffmpeg
caffeinate -i -w $$ & # stay awake while this shell is openIf pgrep finds nothing, the first one fails with an error because -w gets no PID.
Check that caffeinate is working
macOS lists every active assertion with pmset. This only reads information and changes nothing:
pmset -g assertionsThe top part shows each assertion type with a 1 if something holds it (for example PreventUserIdleSystemSleep 1 while caffeinate -i runs). Below that, assertions are listed by the process that owns them. To see only caffeinate's:
pmset -g assertions | grep -i caffeinateIf nothing comes back, caffeinate isn't running. If you expected -d and only see the system assertion, check the flags you typed.
The same list helps the other way round: when your Mac won't sleep, it shows which process is holding it awake. A forgotten background caffeinate shows up here, and killall caffeinate clears it. Our guide to finding what keeps a Mac awake covers the other usual suspects.
Why your Mac still sleeps with caffeinate running
You closed the lid. Closing a MacBook's lid puts it to sleep unless it's connected to power and an external display (clamshell mode). Don't count on any caffeinate flag to keep a MacBook awake with the lid closed and no external display. See clamshell mode on Mac for the built-in way.
You put it to sleep yourself. The idle assertions (-i, -d) only stop idle sleep. Choosing Sleep from the Apple menu still puts the Mac to sleep.
Only the screen turned off. With plain caffeinate or -i, the display sleeps on its normal timer and the screen may lock, but the Mac keeps running. Use -d if you need the screen on.
caffeinate already exited. It stops when you press ⌃C, when you close its Terminal window, when the -t time runs out, when the wrapped command or watched PID finishes, or after 5 seconds for -u without -t. Run the pmset check above to confirm it's still there.
You're on battery with -s. -s only applies on AC power. Use -i on battery.
Something else is scheduled. Don't expect caffeinate to override a scheduled sleep or shutdown, or automatic log-out after inactivity. List scheduled power events with:
pmset -g schedFor automatic log-out, open System Settings and search for "log out". For the sleep timers themselves, see Mac sleep settings explained.
When a menu bar app or glowmoth run fits better
caffeinate is enough when you're already in Terminal and the job has a clear end: one download, one build, one render. It's free, built in and leaves no settings behind.
It gets awkward when:
- You forget it's running. A background
caffeinategives no visible sign. - You want keep-awake to start on its own, for example while an app is open, during work hours, or while a coding agent is busy.
- You're on a laptop and want it to pause if the battery gets low or the Mac runs hot.
For those, a menu bar app does the job better. We make Glowmoth, a keep-awake app that includes a glowmoth command-line tool with close equivalents to most caffeinate uses:
| caffeinate | Glowmoth equivalent |
|---|---|
caffeinate -i | glowmoth on --no-display |
caffeinate -d | glowmoth on --display |
caffeinate -t 7200 | glowmoth for 2h |
| (no equivalent) | glowmoth until 18:00 |
caffeinate -w 4321 | glowmoth wait 4321 |
caffeinate -i make | glowmoth run -- make |
-s, -m, -u | No equivalent |
glowmoth on uses the default duration from Glowmoth's settings, which is Indefinitely unless you change it.
Differences that matter in practice:
glowmoth runreturns the command's exit code and passes ⌃C to it. If the Glowmoth app isn't running,runandwaithold their own power assertion, so a script still works.- Durations are written the way you'd say them (
90,1h30,2h 15m), not in seconds. - The menu bar icon and panel show that the Mac is being kept awake and why. In
pmset -g assertions, Glowmoth's assertion is named after its reason, such as "Glowmoth: Session for 1 hour". - The app can pause keep-awake below a battery level or when the Mac runs hot, and runs rules such as "on AC power and external display connected". MacBooks also get an optional closed-lid mode, which installs a small helper you approve in System Settings.
- The panel's Show More section lists other apps holding the Mac awake, and labels a running
caffeinateas "caffeinate".
Trade-offs: Glowmoth has no equivalent of -s, -m or -u (for external drives it writes a small hidden file instead of using a disk-idle assertion). You install the command-line tool first from Settings ▸ Advanced ▸ Command Line with Install, which places it in ~/.local/bin. You may need to add that folder to your shell's PATH. The app needs an active trial or license to keep the Mac awake. The glowmoth command-line docs list every command and exit code.
Glowmoth is part of MacSuite; you can try it free for 7 days.
To compare free menu bar apps, see Caffeine for Mac alternatives. For every way to stop sleep, including System Settings, see how to stop your Mac from going to sleep.
FAQ
Does caffeinate need sudo?
No. Any user can run it, and the assertion is released when the process ends.
Is caffeinate safe to use?
Yes. It doesn't change system settings and only holds an assertion while it runs. The main risk is battery drain or heat if you forget it on a laptop, so prefer -t or a wrapped command over an open-ended caffeinate.
Does caffeinate work on battery?
-i, -d and -m work on battery. -s only applies on AC power.
How do I stop a caffeinate I started in the background?
Run killall caffeinate, or kill %1 in the Terminal window that started it. Then run pmset -g assertions | grep -i caffeinate to confirm it's gone.