Skip to content
Home » How to Prevent Your Mac from Sleeping (caffeinate)

How to Prevent Your Mac from Sleeping (caffeinate)

macOS has a built-in command for keeping your Mac awake — caffeinate — so there’s no app to install. Its most useful trick is wrapping a command so the Mac stays up for exactly as long as that command runs, then goes back to normal on its own. Here are the everyday forms, how to see what’s already keeping your Mac awake, and how to make the change permanent. Checked on macOS 26.5.2.

Keep it awake right now

Run caffeinate on its own and the Mac stays awake until you stop it with Ctrl-C. Flags decide what stays awake:

  • caffeinate -d — keep the display on
  • caffeinate -i — prevent idle system sleep (the display may still sleep)
  • caffeinate -m — keep the disk from idle-sleeping
  • caffeinate -s — prevent system sleep, but only while on AC power

Combine them as you’d expect — caffeinate -di keeps both the display and the system awake until you quit it.

Keep it awake while a command runs

This is the form worth remembering. Give caffeinate a command, and the Mac stays awake for exactly as long as that command takes, then releases automatically when it finishes:

caffeinate -i ./long-backup.sh

No timer to guess and nothing to clean up — when the script exits, the Mac can sleep again. You can also attach to a process that’s already running by its PID:

caffeinate -w 12345      # stay awake until process 12345 exits

Keep it awake for a set time

When you just want a fixed window, -t takes a number of seconds and then stops on its own:

caffeinate -t 3600       # stay awake for one hour

See what’s keeping it awake

The reverse problem — a Mac that won’t sleep and you don’t know why — has a direct answer. Ask the power manager which processes are holding a “stay awake” assertion:

$ pmset -g assertions
 PreventUserIdleDisplaySleep    0
 PreventUserIdleSystemSleep     1
   pid 12345(caffeinate): ... named: "caffeinate command-line tool"

The counts at the top show whether anything is preventing display or system sleep, and the named lines below point to the culprit — a running caffeinate, a video call, an active download. It’s the quickest way to track down a stray caffeinate you left running.

Make it permanent

caffeinate is designed to be temporary. To change the sleep behavior for good — for instance, never idle-sleep while plugged in — use pmset instead:

sudo pmset -c sleep 0     # -c = on AC power; 0 = never idle-sleep

-b targets battery power and -a targets both. To undo it, set a sensible timeout back, such as sudo pmset -c sleep 10 for ten minutes. For a lighter touch, though, reach for caffeinate — it’s the on-demand tool, while pmset rewrites your standing power settings. (pmset also reports battery state with pmset -g batt, one of the Mac terminal commands worth knowing.)

So: caffeinate on its own to stay awake until Ctrl-C, wrap a command to keep the Mac up only while it runs, -t for a fixed window, pmset -g assertions to find what’s holding sleep off, and pmset when you want the change to stick. All checked on macOS 26.5.2.

Leave a Reply

Your email address will not be published. Required fields are marked *