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.
| Flag | Assertion created | Effect |
|---|---|---|
| -i | Prevent idle system sleep | Mac stays awake; display may sleep |
| -d | Prevent idle display sleep | Screen stays on |
| -m | Prevent idle disk sleep | Disks keep spinning |
| -s | Prevent system sleep | Stays awake on AC power only |
| -u | Declare user activity | Wakes the display; defaults to a short timeout |
| -t seconds | Timeout for all assertions | Releases assertions after the given time |
| -w pid | Wait for a process ID | Holds assertions until that process exits |
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.
| Task | Command | Why it fits |
|---|---|---|
| Long software build | caffeinate -i make | Awake exactly while the build runs |
| Large download already running | caffeinate -i -w 4821 | Attaches to the existing process ID |
| Presentation for one hour | caffeinate -d -t 3600 | Screen stays on for a set time |
| Overnight backup on a desktop | caffeinate -s ./backup.sh | Blocks all system sleep on AC power |
| Remote SSH work session | caffeinate -i in a separate shell | Prevents idle sleep that drops the connection |
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.
| Method | Scope | Persists after reboot | Best for |
|---|---|---|---|
| caffeinate | One task or time window | No | Scripts and ad hoc jobs |
| pmset | System-wide energy settings | Yes | Administrators setting policy |
| Battery and Energy settings | Graphical sleep preferences | Yes | Everyday users |
| Third-party menu bar apps | Usually a toggle or timer | Depends on the app | Users who avoid Terminal |
| App-created assertions | Set by the running app | No | Video playback and calls |
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.
| Symptom | Cause | Fix |
|---|---|---|
| Mac sleeps when lid closes | Lid closure overrides assertions | Keep the lid open or use clamshell mode |
| Mac sleeps on battery with -s | -s works only on AC power | Use -i or connect the charger |
| Screen turns off during a talk | Only -i was used | Add -d to keep the display on |
| Mac never sleeps afterward | caffeinate left running in background | Find it with pmset -g assertions and stop it |
| Job continues but network drops | Wi-Fi power saving or remote server timeout | Check network settings separately |
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.