The short answer
Put caffeinate -i in front of the command: caffeinate -i make build keeps the Mac from idle sleeping until the build finishes, then lets it sleep normally. Use caffeinate -i -w <pid> to wait for a process that is already running, or caffeinate -i -t 3600 for a set time. Check it with pmset -g assertions. Closing a MacBook's lid still puts it to sleep unless it's in clamshell mode with power and an external display.
On this page
Wrap the command with caffeinate
caffeinate -i xcodebuild -scheme App buildcaffeinate holds a power assertion while the command runs and releases it when the command exits, so you never forget to turn it off. -i prevents idle sleep; the display can still turn off, which saves power.
| Option | What it does |
|---|---|
-i | Prevent idle system sleep. The usual choice for builds and downloads |
-d | Keep the display on too, for a dashboard you're watching |
-s | Prevent system sleep, only while on AC power |
-t <seconds> | Stay awake for a set time |
-w <pid> | Stay awake until the process with this ID exits |
-i- What it does
- Prevent idle system sleep. The usual choice for builds and downloads
-d- What it does
- Keep the display on too, for a dashboard you're watching
-s- What it does
- Prevent system sleep, only while on AC power
-t <seconds>- What it does
- Stay awake for a set time
-w <pid>- What it does
- Stay awake until the process with this ID exits
For a task that's already running
pgrep -n node
caffeinate -i -w 48213Check that it worked
pmset -g assertions | grep -A1 caffeinatecaffeinate appears under Listed by owning process with PreventUserIdleSystemSleep while it's active. See sleep assertion for the other types.
What a closed lid does
Closing a MacBook's lid puts it to sleep whatever caffeinate says, unless the Mac is connected to power and an external display (clamshell mode). Leave the lid open for long builds on battery, and turn the display brightness down instead.