How to stop a MacBook from sleeping

You want a long download to finish, a render to complete, or a script to run overnight — and macOS keeps going to sleep. There are six ways to stop it, and the right one depends on whether the lid is open.

On this page

  1. System Settings (the obvious one)
  2. caffeinate, the built-in command
  3. Staying awake with the lid closed
  4. pmset, for permanent changes
  5. What this costs you

System Settings (the obvious one)

Go to System Settings → Lock Screen and set "Turn display off when inactive" to Never. Then System Settings → Battery (or Energy Saver on a desktop) and turn on "Prevent automatic sleeping when the display is off" while on power.

This is enough for most cases and it is reversible from the same place. Its limitation is that it is a global setting: you will forget you set it, and the Mac will never sleep again until you remember.

caffeinate, the built-in command

macOS ships a command that exists solely for this. Open Terminal:

caffeinate -d

That prevents the display from sleeping for as long as the command runs. Press Control-C to stop. The useful flags:

  • -d prevent the display sleeping
  • -i prevent idle system sleep
  • -s prevent system sleep — mains power only
  • -m prevent disks spinning down
  • -u declare user activity, as if you had touched the keyboard
  • -t 3600 do it for 3600 seconds, then stop by itself

The best habit is the timeout. caffeinate -dt 7200 keeps the display awake for two hours and then releases — so a forgotten terminal window cannot flatten the battery overnight.

You can also tie it to a specific job. This keeps the Mac awake for exactly as long as the command takes and not a second longer:

caffeinate -i ./my-long-script.sh

And you can attach it to an already-running process by PID:

caffeinate -w 54321

Staying awake with the lid closed

This is the case people actually search for, and the honest answer has a caveat: macOS has no supported setting for "stay awake with the lid shut and no external display". Closed-display operation officially requires an external display, a keyboard and a pointing device — that is clamshell mode.

What does work without a monitor:

caffeinate -s

Leave that running and close the lid. The built-in display sleeps, but the system stays awake and your work continues. It needs mains power — -s is ignored on battery by design.

Watch the temperature

A MacBook working hard with the lid shut sheds heat through a smaller surface area. For a download or a backup this is irrelevant. For a multi-hour render, open the lid or put the machine on a stand.

pmset, for permanent changes

pmset edits the power management settings directly. It needs sudo and it persists across reboots, which makes it both the most powerful option and the easiest to regret.

# never sleep on mains power
sudo pmset -c sleep 0

# display off after 30 minutes on mains
sudo pmset -c displaysleep 30

# see everything currently set
pmset -g

To undo, set the values back rather than trying to remember defaults:

sudo pmset -c sleep 10 displaysleep 10

-c means mains power, -b battery, -a both. Use -c. Disabling sleep on battery is how you find a hot, flat laptop in your bag.

What this costs you

Preventing sleep keeps the CPU powered and, with -d, the display lit too. A display is typically the largest single power draw in a laptop, so caffeinate -i (system only) costs far less than caffeinate -d (display too). If your goal is a download finishing, you almost never need the display.

Battery health is a second-order concern: heat is what ages lithium cells, and a Mac kept artificially awake runs warmer than one that sleeps. Occasional use is nothing to worry about. Permanent pmset changes on battery are worth avoiding.

A different thing to do with a closing lid

Newer MacBooks report their hinge angle continuously. Still reads it and folds your desktop as the lid comes down, like the iPhone Duo shutting. Free, open source, and it needs no permission beyond a single screenshot. How the sensor works →