caffeinate — Prevent Your Mac from Sleeping
Practical guide to caffeinate: keep your Mac and display awake and prevent idle sleep during long builds, downloads or presentations.
caffeinate is a small but genuinely handy macOS command: it keeps your Mac awake and stops the display or the system from drifting into sleep. That is invaluable when a long build is running, a large download must not be interrupted, or you are giving a presentation and the screen keeps dimming. Short flags let you control exactly which kind of sleep is blocked – and you can wrap caffeinate around another command so the Mac stays awake only for as long as that task runs.
Basic Usage
caffeinate — Prevent sleep until you press Ctrl+C.
caffeinatecaffeinate -t <seconds> — Prevent sleep for a specific duration.
caffeinate -t 3600caffeinate <command> — Prevent sleep while a command runs.
caffeinate make buildcaffeinate -w <pid> — Prevent sleep while a process is running.
caffeinate -w $(pgrep rsync)Sleep Assertion Types
caffeinate -d — Prevent the display from sleeping.
caffeinate -dcaffeinate -i — Prevent idle sleep (system stays awake).
caffeinate -icaffeinate -s — Prevent system sleep (even when on AC power lid close is allowed).
caffeinate -scaffeinate -m — Prevent disk from going idle.
caffeinate -mcaffeinate -dims — Combine multiple assertions (display + idle + disk + system).
caffeinate -dimsCommon Patterns
caffeinate -i rsync -avz <src> <dest> — Keep system awake during a large file transfer.
caffeinate -i rsync -avz ~/Photos/ /Volumes/Backup/Photos/caffeinate -d -t 7200 — Keep display on for a 2-hour presentation.
caffeinate -d -t 7200caffeinate -i make -j$(sysctl -n hw.ncpu) — Keep system awake during a long compilation.
caffeinate -i make -j$(sysctl -n hw.ncpu)caffeinate -is npm run build — Keep system awake during a long build process.
caffeinate -is npm run build Conclusion
caffeinate is a classic Unix tool: one clearly defined job, done with a handful of flags. The most elegant way to use it is to put the actual command right after it – then the Mac stays awake exactly as long as the task takes and automatically returns to its normal power-saving behaviour afterwards. There is nothing to change in System Settings and nothing to undo later. For permanent changes to sleep behaviour, pmset remains the better tool; caffeinate is the quick, temporary solution for the moment.
Further Reading
- Change Energy Saver settings on Mac – Apple Support on sleep and power options
- caffeinate(8) – manual pages – detailed reference for every flag