How to prevent a MacBook from sleeping
pmset alone is not enough on a MacBook — the system still sleeps on lid-close or idle user-session timeouts. You need pmset (persistent settings) and a long-running caffeinate -simdu (active sleep assertion).
sudo pmset -a sleep 0 disksleep 0 hibernatemode 0 standby 0 autopoweroff 0 powernap 0sudo pmset -a disablesleep 1
brew install tmuxtmux new -d -s caffeinate 'caffeinate -simdu'Read on for what each piece does and alternatives to tmux.
1. Persistent settings with pmset
Section titled “1. Persistent settings with pmset”Plug in AC power, then:
sudo pmset -a sleep 0 disksleep 0 hibernatemode 0 standby 0 autopoweroff 0 powernap 0sudo pmset -a disablesleep 1 # allow lid-closed operation without an external displayVerify:
pmset -g | grep -E 'sleep|hibernatemode|standby'2. Active assertion with caffeinate
Section titled “2. Active assertion with caffeinate”Keep a caffeinate process running for as long as you want the Mac awake:
caffeinate -simduFlags:
| Flag | Prevents |
|---|---|
-s | system sleep (AC) |
-i | idle sleep |
-m | disk sleep |
-d | display sleep |
-u | declares user activity |
Pick whichever way keeps it running long enough for your use case.
Option A: Dedicated terminal window
Section titled “Option A: Dedicated terminal window”Open a new Terminal/iTerm/Ghostty window and run:
caffeinate -simduLeave the window open. Closing the window or pressing Ctrl-C stops it.
Option B: nohup (background, survives logout)
Section titled “Option B: nohup (background, survives logout)”nohup caffeinate -simdu >/dev/null 2>&1 &disownCheck it is running:
pgrep -fl caffeinateStop it:
pkill caffeinateOption C: tmux
Section titled “Option C: tmux”brew install tmux # if not installedtmux new -d -s caffeinate 'caffeinate -simdu'Reattach to inspect:
tmux attach -t caffeinateStop it:
tmux kill-session -t caffeinateOption D: screen
Section titled “Option D: screen”screen -dmS caffeinate caffeinate -simduReattach:
screen -r caffeinateStop it:
screen -S caffeinate -X quit3. Verify
Section titled “3. Verify”pmset -g assertionsYou should see PreventUserIdleSystemSleep and PreventSystemSleep held by caffeinate.
Revert
Section titled “Revert”pkill caffeinatesudo pmset -a restoredefaultsTroubleshooting
Section titled “Troubleshooting”- Still sleeps on lid close: confirm
disablesleepis1inpmset -g. - Docker hangs after wake:
disksleepmust be0; restart Docker Desktop. - Something else forces sleep: check
pmset -g assertionsfor other processes (backup tools, VPNs) holding conflicting assertions.