caffeinate

On this page 7

The caffeinate command is a built-in macOS command-line utility that prevents the Mac from sleeping by creating power management assertions, either for a fixed time, until it is stopped, or for as long as a specified process runs. In Terminal & scripting, it is the standard way to keep a machine awake during long downloads, builds, backups or remote sessions without changing the system’s energy settings permanently.

Apple has shipped caffeinate since OS X 10.8 Mountain Lion. Its strength is that it is temporary and scoped: when the command ends, the assertion disappears and normal sleep behavior returns, which makes it safer than editing global settings with pmset.

How caffeinate uses power assertions

macOS decides when to sleep through the power management subsystem, which checks whether any process holds an assertion that blocks a particular kind of sleep. Applications such as video players create these assertions automatically; caffeinate exposes the same mechanism to the command line.

When caffeinate runs, it registers one or more assertions with the system and then waits. The assertions are released when the timeout expires, when the watched process exits, or when the user presses Control-C. Running pmset -g assertions shows each active assertion and the process that owns it, which is the most reliable way to confirm that caffeinate is working.

If no flags are given, caffeinate creates an assertion that prevents idle system sleep and keeps it until interrupted. The display can still turn off, which is often exactly what an unattended job needs.

Options and the assertions they create

Each caffeinate flag maps to a specific assertion type, and flags can be combined. Choosing the narrowest assertion keeps energy use and screen wear to a minimum. The table below lists the options documented in the manual page.

FlagAssertion createdEffect
-iPrevent idle system sleepMac stays awake; display may sleep
-dPrevent idle display sleepScreen stays on
-mPrevent idle disk sleepDisks keep spinning
-sPrevent system sleepStays awake on AC power only
-uDeclare user activityWakes the display; defaults to a short timeout
-t secondsTimeout for all assertionsReleases assertions after the given time
-w pidWait for a process IDHolds assertions until that process exits
caffeinate flags and their effects

A command can also be passed directly, as in caffeinate -i rsync -a src/ dest/. In this form, caffeinate runs the utility as a child process and holds the assertion exactly as long as the utility runs, which is the cleanest pattern for scripts.

Common caffeinate patterns

Most real uses of caffeinate fall into a few repeatable patterns. The following table pairs typical tasks with a suitable command and the reason it fits.

TaskCommandWhy it fits
Long software buildcaffeinate -i makeAwake exactly while the build runs
Large download already runningcaffeinate -i -w 4821Attaches to the existing process ID
Presentation for one hourcaffeinate -d -t 3600Screen stays on for a set time
Overnight backup on a desktopcaffeinate -s ./backup.shBlocks all system sleep on AC power
Remote SSH work sessioncaffeinate -i in a separate shellPrevents idle sleep that drops the connection
Typical tasks and recommended commands

In shell scripts, a useful idiom is caffeinate -i -w $$ & near the top. It attaches caffeinate to the script’s own process ID, so the Mac stays awake until the script finishes, even if it exits early because of an error.

How caffeinate compares with other ways to stay awake

caffeinate is one of several tools that influence sleep on a Mac, and each has a different scope. The key distinction is temporary assertion versus permanent configuration.

MethodScopePersists after rebootBest for
caffeinateOne task or time windowNoScripts and ad hoc jobs
pmsetSystem-wide energy settingsYesAdministrators setting policy
Battery and Energy settingsGraphical sleep preferencesYesEveryday users
Third-party menu bar appsUsually a toggle or timerDepends on the appUsers who avoid Terminal
App-created assertionsSet by the running appNoVideo playback and calls
Comparison of methods for preventing sleep

Neighboring tools in macOS automation often appear alongside caffeinate. A launch agent started by launchctl can wrap a scheduled job in caffeinate, and pmset can schedule a wake event so that the job starts at all; caffeinate then keeps the machine awake until it finishes.

Limitations and mistakes when using caffeinate

The most frequent surprise is that caffeinate does not stop a laptop from sleeping when the lid is closed. Closing the lid triggers clamshell sleep, which assertions do not override unless the Mac is in clamshell mode with external power, display and input connected.

SymptomCauseFix
Mac sleeps when lid closesLid closure overrides assertionsKeep the lid open or use clamshell mode
Mac sleeps on battery with -s-s works only on AC powerUse -i or connect the charger
Screen turns off during a talkOnly -i was usedAdd -d to keep the display on
Mac never sleeps afterwardcaffeinate left running in backgroundFind it with pmset -g assertions and stop it
Job continues but network dropsWi-Fi power saving or remote server timeoutCheck network settings separately
Problems, causes and fixes

Two further cautions apply. A forgotten background caffeinate drains the battery and keeps fans running, so a timeout or a watched process is always preferable to an open-ended command. Also, caffeinate controls only sleep; it does not prevent the screen saver password prompt or a forced restart from software updates.

The simple explanation

Your Mac goes to sleep when it thinks nobody is using it. That saves power, but it also stops long jobs such as big downloads. The caffeinate command is a polite note to the Mac saying “stay awake for now”.

You can tell it to stay awake for an hour, or until a particular job is done. When the time runs out or the job finishes, the note is thrown away and the Mac goes back to its usual habits. Nothing is changed permanently, which is why people like using it.

How it is used in practice

A video editor needs to export a 90-minute project and then copy the 40 GB file to a network drive. Her MacBook Pro is set to sleep after 10 minutes of inactivity, and she plans to leave the office while the work completes.

Instead of changing her energy settings, she runs a short shell script that starts with caffeinate -i -w $$ &, waits for the export, and then runs rsync to the server. The assertion stays active only while the script runs. She keeps the lid open and the charger connected.

When she returns the next morning, the file is on the server, the log shows no errors, and pmset -g assertions lists no leftover caffeinate entries. The Mac went to sleep normally after the copy finished, so it did not stay awake all night for no reason.