Keep the Mac awake from Terminal
caffeinate stops the Mac from sleeping for as long as you say, or until a command finishes.
Works on: macOS Big Sur 11 and later · checked on macOS Golden Gate 27
Published: · Source: SS64
When a long download, backup or build must not be interrupted by sleep, Terminal has a built-in answer:
caffeinate
Leave it running and the Mac will not idle-sleep; press ⌃ + C to stop it. The variants are more useful:
caffeinate -t 3600 # stay awake for one hour
caffeinate -d # keep the display on as well
caffeinate -i make # stay awake until the command finishes
caffeinate -w 1234 # stay awake until process 1234 exits
The last two are the ones to remember: the Mac stays awake exactly as long as the job needs, and returns to its normal sleep settings by itself when it ends.
-s keeps the whole system awake, but only while the Mac is on power.
man caffeinate lists every option.
Related tips
Keep the Mac awake automatically with Amphetamine
One click in the menu bar stops the Mac sleeping, and Triggers do it on their own while an app runs or a display is connected.
Stretch the battery with Low Power Mode
Energy Mode trades a little performance for battery life, separately on battery and on the power adapter.
Look after the battery of a Mac that lives plugged in
Optimised charging and a charge limit stop a permanently connected laptop from sitting at 100% all day.