How to Use the caffeinate Command in macOS Terminal to Prevent Mac Sleep During Long Tasks

Mac users frequently rely on third-party menu bar utilities (such as Amphetamine or Caffeine) to prevent their laptops and desktop Macs from entering display sleep or system hibernation during lengthy video exports, Xcode builds, or large cloud file uploads. However, macOS includes an enterprise-grade command-line utility built directly into Darwin called caffeinate. Operating through native power assertions, caffeinate allows you to keep your Mac awake indefinitely, set exact timeout timers, or link wake states directly to running shell processes.

How caffeinate Interacts with Apple Power Assertions

When executed, caffeinate registers a high-priority PreventUserIdleSystemSleep power assertion with the macOS Power Management daemon (pmset). While active, the operating system ignores standard idle timeout triggers. Crucially, when the command finishes or is terminated via Ctrl + C, the assertion is purged instantaneously, restoring your Mac to its normal battery-saving sleep schedules without requiring manual configuration adjustments.

Basic Syntax and Common Operational Switches

The standard syntax for the caffeinate command is straightforward:

caffeinate [options] [command [arguments...]]

The utility supports targeted assertion modifiers:

  • -d: Prevents the display screen from sleeping or dimming.
  • -i: Prevents the system from idle sleeping (the display can turn off, but background CPU tasks continue unimpeded).
  • -m: Prevents mechanical and solid-state disks from spinning down or entering low-power idle states.
  • -s: Prevents system sleep when running specifically on AC wall power.
  • -u: Declares that a user is actively present, resetting idle timeout timers.
  • -t <seconds>: Specifies an exact timeout duration in seconds after which caffeinate automatically terminates.
  • -w <PID>: Waits for a designated Process ID (PID) to exit, keeping the Mac awake until that specific application closes.

Practical caffeinate Examples for macOS Users

1. Keep the Mac Awake Indefinitely

To keep both the computer and display fully awake while you deliver a presentation or monitor a dashboard, open Terminal and run:

caffeinate -d

The command will run in your terminal session until you press Ctrl + C to release the wake lock.

2. Keep Awake for a Specific Duration (Timer Mode)

If you want your Mac to stay awake for exactly 2 hours (7,200 seconds) while performing an off-hours download, pass the -t parameter:

caffeinate -d -t 7200

Once 7,200 seconds elapse, caffeinate exits cleanly and normal system sleep policies resume immediately.

3. Wrap caffeinate Around a Long-Running Command

You can prefix any standard shell command or script with caffeinate. The Mac will stay awake while the command executes and return to normal sleep behavior the split-second the command completes:

caffeinate -i rsync -avz /Volumes/Data/ /Volumes/Backup/
caffeinate -d make -j8

4. Bind caffeinate to a Running Background Process ID

If a GUI application like Final Cut Pro, Blender, or Xcode is already rendering a project, identify its Process ID using pgrep and attach caffeinate directly:

pgrep Blender
caffeinate -w 45821

caffeinate will monitor PID 45821. As soon as Blender finishes its render pipeline and exits, caffeinate releases the system power assertion automatically.

Verifying Active caffeinate Assertions via Terminal

To confirm that caffeinate has successfully registered a wake lock with macOS, run:

pmset -g assertions | grep caffeinate

The output reports the exact Process ID of caffeinate alongside its declared assertion type, providing verifiable confirmation that your Mac will not unexpectedly sleep during mission-critical tasks.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.