caffeinate on Mac: keep it awake from Terminal

caffeinate is a command built into macOS that keeps the Mac from sleeping while it runs. caffeinate -i keeps it awake until you press Control-C, caffeinate -i -t 3600 for an hour, caffeinate -i make for as long as make runs, and caffeinate -i -w 12345 until process 12345 exits. Add -d to keep the display on as well. It needs no administrator password and changes no settings.

Updated · commands checked on macOS 27.2

Keep the Mac awake for an hour

  1. Open Terminal from Applications › Utilities.
  2. Type caffeinate -i -t 3600 and press Return. -i prevents idle sleep; -t 3600 stops it after 3600 seconds.
  3. In a second Terminal window, run pmset -g assertions and look for caffeinate command-line tool.
  4. Press Control-C in the first window, or close it. The assertion is released and the Mac sleeps as usual.

The flags

From man caffeinate. The last column is the assertion each flag created when we ran it with -t 3 and looked at pmset -g assertions. You can combine them: -di created two.

FlagWhat it doesShows up as
(none)Prevents idle sleep (the default when no flag is given).PreventUserIdleSystemSleep
-iPrevents the system from idle sleeping. The display can still turn off.PreventUserIdleSystemSleep
-dPrevents the display from sleeping.PreventUserIdleDisplaySleep
-sPrevents the system from sleeping. Valid only on AC power, the man page says.PreventSystemSleep
-mPrevents the disk from idle sleeping.PreventDiskIdle
-uDeclares that the user is active: turns the display on if it’s off. Without -t it lasts 5 seconds.UserIsActive
-t NReleases the assertions after N seconds. Ignored when you give caffeinate a command to run.—
-w PIDWaits for that process to exit, then releases. Ignored when you give a command.—

Apple’s power-management header explains the difference between the first two rows and the third: with PreventUserIdleSystemSleep “the display may dim and idle sleep”, but the system may not.

Examples

CommandKeeps the Mac awake
caffeinateUntil you press Control-C.
caffeinate -i -t 7200Two hours. The display can turn off.
caffeinate -d -i -t 180030 minutes, with the display on.
caffeinate -i makeWhile make runs, then it exits too.
caffeinate -i -w 12345Until process 12345 exits.

When you give caffeinate a command, it holds the assertion for as long as that command runs, then exits with it. That’s the most reliable way to cover a long job, because you don’t have to guess how long it will take. More in keep your Mac awake until a command finishes.

To find the number for -w, use pgrep: pgrep -n -x ffmpeg prints the newest process called exactly “ffmpeg”.

Check that it’s working

pmset -g assertions | grep -A3 caffeinate

With caffeinate -t 3 running (shortened):

   pid 53757(caffeinate): […] 00:00:00 PreventUserIdleSystemSleep named: "caffeinate command-line tool"
	Details: caffeinate asserting for 3 secs
	Localized=THE CAFFEINATE TOOL IS PREVENTING SLEEP.
	Timeout will fire in 2 secs Action=TimeoutActionRelease

With caffeinate -i sleep 3 running, the details name the command instead (shortened):

   pid 53793(caffeinate): […] 00:00:00 PreventUserIdleSystemSleep named: "caffeinate command-line tool"
	Details: caffeinate asserting on behalf of 'sleep' (pid 53791)

pmset -g also lists it. With caffeinate -d running, our display line read:

 displaysleep         10 (display sleep prevented by caffeinate, Telegram)

When caffeinate isn’t enough

It lives in a Terminal window. Close the window, quit Terminal or log out, and it stops. That’s safe, but it’s easy to do by accident halfway through a long job.

A wrong PID fails silently. We ran caffeinate -w with a process number that didn’t exist: it exited at once, with no message and exit status 0, and the Mac was free to sleep.

-t is a guess. Too short and the Mac sleeps before the job is done; too long and it stays awake for nothing. It also does nothing when you pass a command (the man page says the timeout isn’t used then).

It doesn’t tell you when it’s done, and it can’t wait for a browser download or a Time Machine backup.

The lid still wins. Closing a MacBook’s lid puts it to sleep unless it’s in closed-display mode.

SleepPilot (Mac App Store) covers the middle three. Its Until done… option lets you pick a command that’s already running in Terminal, a download in your Downloads folder or a running Time Machine backup; when that finishes, it lets the Mac sleep and sends a notification. It doesn’t change the lid rule either.

Questions

Does caffeinate need sudo?

No. Every example on this page was run as a normal user. caffeinate changes no settings; it only holds an assertion while it runs.

How do I stop caffeinate?

Press Control-C in its Terminal window or close the window. We checked both: the assertion disappears from pmset -g assertions straight away. With -t or -w it also stops by itself.

Does caffeinate work on battery?

Yes for -i and -d. The man page says -s is valid only when the Mac is on AC power.

Does caffeinate keep a MacBook awake with the lid closed?

Don’t count on it. Apple’s power-management header says a Mac that is prevented from idle sleeping can still sleep when the lid closes. The man page doesn’t mention the lid for -s either. See lid closed.

What is “caffeinate” in pmset or Activity Monitor when I didn’t run it?

A script or an app started it for you. pmset -g assertions shows its PID; ps -p with the PID and -o ppid= shows which process started it. More about caffeinate.

Sources

Related

SleepPilot for Mac

Keeps your Mac awake until the job is done, then lets it sleep. Shows which apps keep it awake when it should be sleeping.

Coming soon to the Mac App Store

macOS 14 Sonoma or later · Apple silicon and Intel