macOS includes caffeinate, a small command-line utility that temporarily changes sleep behaviour. It is useful during a download, build, backup, remote session, or any other task that should continue while the Mac is unattended. On battery power, the important part is choosing an assertion that actually applies while the charger is disconnected.
Keep an unplugged Mac awake
Open Terminal and run:
caffeinate -i
The -i flag prevents idle system sleep. The command remains in the foreground until you stop it with Control-C, close that Terminal session, or otherwise terminate the process.
The short versionUse caffeinate -i on battery. The -s assertion prevents system sleep only while the Mac is connected to AC power.
Running plain caffeinate with no assertion flags also prevents idle system sleep. Writing -i explicitly makes the intention clearer, especially in a script or saved command.
Choose the right assertion
Sleep is not one indivisible state. caffeinate exposes several assertions, and they can be combined:
-iprevents idle system sleep and is the usual choice for work that must continue on battery.-dprevents display sleep. Usecaffeinate -idonly when the screen must remain visible.-mprevents idle disk sleep. Modern Macs rarely need this as a general-purpose addition.-sprevents system sleep, but its assertion is valid only on AC power.-udeclares user activity, wakes an off display, and defaults to a five-second timeout unless-tis supplied.
Keeping the display awake consumes considerably more battery than allowing it to turn off while the system continues working. For an unattended transfer or build, -i is normally enough.
Set a time limit
Use -t to specify a duration in seconds. This keeps the Mac awake for one hour:
caffeinate -i -t 3600
After the timer expires, the assertion is released automatically and the normal Energy settings apply again. A timeout is preferable when the task has a predictable upper bound and there is no reason to keep the Mac awake all night.
Follow a command or process
A safer pattern is to place the real command after caffeinate. The assertion then lasts exactly as long as that command:
caffeinate -i rsync -av ~/Documents/ /Volumes/Backup/Documents/
Arguments after the utility name belong to that utility. When a utility is supplied, -t is not used, because the utility's lifetime controls the assertion.
If the work is already running, find its process ID and use -w:
caffeinate -i -w 12345
Replace 12345 with the actual PID. The assertion disappears when that process exits. This is useful for a long-running GUI application or a command that was started before you remembered caffeinate.
Run it in the background
For a timed session that should not occupy the Terminal prompt, start it as a background job:
caffeinate -i -t 3600 &
echo $!
The second command prints the PID of the backgrounded caffeinate process. Keep that number if you may want to end the session early:
kill PID
A background job can still end when its shell exits. For work that matters, wrapping the actual command with caffeinate is easier to reason about than creating an independent assertion and hoping both processes have matching lifetimes.
Check the assertion
Inspect current power-management assertions with:
pmset -g assertions
Look for PreventUserIdleSystemSleep and an entry owned by caffeinate. The exact output contains other assertions created by macOS and applications, so the global count may already be non-zero before your command starts.
You can also confirm that the process exists:
pgrep -fl caffeinate
Know the limits
- Closing a MacBook lid normally causes sleep.
caffeinate -iis not a supported way to override lid-close behaviour. - An assertion prevents idle sleep; it does not override a critically low battery, shutdown, restart, or an explicit Sleep command.
- Battery drain continues while work runs, and rises substantially if the display is kept awake.
- If the Terminal or remote shell that owns a foreground
caffeinateprocess closes, do not assume the assertion survived. Verify it.
For most unplugged use, start with caffeinate -i. Add a timeout when you know the duration, or attach the assertion directly to the command so it ends at the right moment without manual cleanup.