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.

caffeinate

caffeinate -t <seconds> — Prevent sleep for a specific duration.

caffeinate -t 3600

caffeinate <command> — Prevent sleep while a command runs.

caffeinate make build

caffeinate -w <pid> — Prevent sleep while a process is running.

caffeinate -w $(pgrep rsync)

Sleep Assertion Types

caffeinate -d — Prevent the display from sleeping.

caffeinate -d

caffeinate -i — Prevent idle sleep (system stays awake).

caffeinate -i

caffeinate -s — Prevent system sleep (even when on AC power lid close is allowed).

caffeinate -s

caffeinate -m — Prevent disk from going idle.

caffeinate -m

caffeinate -dims — Combine multiple assertions (display + idle + disk + system).

caffeinate -dims

Common 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 7200

caffeinate -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

  • defaults – read and write macOS user preferences
  • diskutil – manage disks, volumes and partitions
  • dscl – command-line access to macOS directory services