[READ-ONLY] Mirror of https://github.com/mayask/mac-lock-sleep. Save energy: force a lidless Mac to sleep when you lock the screen - even wi
README.md

mac-lock-sleep #

Save energy: force a lidless Mac to sleep when you lock the screen — even with Chrome, Docker, and background jobs holding it awake.

Put a macOS machine to sleep a fixed delay after the screen is locked — even when Chrome, Docker, USB audio, or a long-running agent session would otherwise keep it awake.

Who needs this #

Lidless Macs — Mac Studio, Mac mini, iMac, Mac Pro. They have no lid to close, so there is no built-in "force sleep now" gesture. Combined with idle-sleep assertions from browsers/Docker, they stay awake 24/7.

MacBooks in clamshell mode (lid closed, external display, on power). The lid is already closed so it can't be re-closed — functionally a lidless Mac.

MacBooks used normally (lid open) don't need this — closing the lid sleeps them instantly, regardless of assertions. It's safe to install on a MacBook but redundant unless you run clamshell.

The problem #

macOS idle-sleep is vetoed by PreventUserIdleSystemSleep / NoIdleSleepAssertion holders. On a work machine with a browser playing audio, a WebRTC call, a USB audio interface, or Docker running, those assertions sit active forever, so the sleep timer never fires and the machine draws power 24/7. There is no "real hibernate" on Apple Silicon to fall back on.

mac-lock-sleep makes locking the screen an explicit, reliable "you may sleep now" gesture. Locking is something you already do when you walk away, and it is never something you do while actively working — so it is a clean signal that the machine can go down.

How it works #

  • Runs as a per-user LaunchAgent in /Library/LaunchAgents/, so every logged-in user gets an instance.
  • Each instance listens for the system-wide distributed notifications posted by loginwindow:
    • com.apple.screenIsLocked → arm a one-shot timer for LOCK_SLEEP_DELAY seconds (default 1800 = 30 min).
    • com.apple.screenIsUnlocked → cancel the timer.
  • When the timer fires it checks that nobody has touched the keyboard/mouse recently (system-wide HID idle ≥ LOCK_SLEEP_IDLE_GATE, default 120 s) and then runs pmset sleepnow.
  • pmset sleepnow is a user-requested sleep, which bypasses idle-sleep assertions (PreventUserIdleSystemSleep, NoIdleSleepAssertion). Only the stronger PreventSystemSleep can veto it — see verification below.

The HID-idle gate handles the multi-user case: lock user A, fast-user-switch to user B and keep working. HID idle is system-wide, so every instance sees B's activity and skips the sleep instead of dozing off over active work. The machine only sleeps if nobody has touched any input for the gate window when the timer fires.

What survives sleep #

pmset sleepnow does a normal sleep — RAM stays powered, the SoC drops to a low-power state. On wake, every process resumes exactly where it was suspended:

  • tmux sessions, Docker containers, local web servers, browser tabs — all alive in RAM, resumed mid-execution. Nothing is killed or restarted.
  • External/network connections (SSH to remote hosts, WebSockets, DB pools) may time out during sleep and need reconnecting. Your local processes are fine; it's the remote end of long-lived TCP connections that may have given up.
  • USB peripherals usually re-enumerate cleanly; the occasional audio interface may need a replug.

Requirements #

  • macOS (developed/tested on macOS 14 Sonoma, Apple Silicon).
  • Swift compiler: xcode-select --install if swiftc is not on your path.

Install #

cd ~/src/mac-lock-sleep      # or wherever you cloned it
./install.sh

install.sh builds the binary, then uses sudo to copy:

  • mac-lock-sleep → /usr/local/bin/mac-lock-sleep
  • com.user.mac-lock-sleep.plist → /Library/LaunchAgents/com.user.mac-lock-sleep.plist

It then loads the agent into your current session. Other users get it automatically on their next login — nothing extra to do.

Verify it works #

1. Confirm it is loaded and listening:

launchctl list | grep mac-lock-sleep          # should list com.user.mac-lock-sleep
tail ~/Library/Logs/mac-lock-sleep.log        # should end with "mac-lock-sleep ready ..."

2. Confirm nothing can veto a forced sleep:

pmset -g assertions | grep PreventSystemSleep

This must read PreventSystemSleep 0. mac-lock-sleep uses pmset sleepnow, which ignores idle-sleep assertions (Chrome media, USB audio, Docker, agent sessions) but is still vetoed by PreventSystemSleep. On a normal desktop this is always 0; if it is not, find and close the holder before relying on forced sleep.

3. Real end-to-end test (recommended):

Temporarily shorten the delay so you do not wait 30 minutes:

sudo sed -i '' 's/<string>1800<\/string>/<string>60<\/string>/' \
    /Library/LaunchAgents/com.user.mac-lock-sleep.plist
sudo launchctl bootout gui/$(id -u)/com.user.mac-lock-sleep 2>/dev/null
sudo launchctl bootstrap gui/$(id -u) /Library/LaunchAgents/com.user.mac-lock-sleep.plist

Now keep Chrome and Docker running (ideally with a tab playing audio), lock the screen (Ctrl-Cmd-Q), and walk away. Within ~60 s the Mac should sleep despite the audio/Docker assertions. Wake it, unlock, and restore the 30-minute delay:

sudo sed -i '' 's/<string>60<\/string>/<string>1800<\/string>/' \
    /Library/LaunchAgents/com.user.mac-lock-sleep.plist
sudo launchctl bootout gui/$(id -u)/com.user.mac-lock-sleep 2>/dev/null
sudo launchctl bootstrap gui/$(id -u) /Library/LaunchAgents/com.user.mac-lock-sleep.plist

The log records every step:

tail ~/Library/Logs/mac-lock-sleep.log
# [..] mac-lock-sleep ready (delay=60s idleGate=120s dryRun=false) — waiting for screen lock
# [..] screen locked — armed sleep in 60s
# [..] putting machine to sleep (HID idle 62s)
# [..] pmset sleepnow exited status=0

Configure #

Edit the environment variables in /Library/LaunchAgents/com.user.mac-lock-sleep.plist and reload the agent (bootout

  • bootstrap, as above):
Variable Default Meaning
LOCK_SLEEP_DELAY 1800 Seconds after lock before sleeping.
LOCK_SLEEP_IDLE_GATE 120 Min system-wide HID-idle seconds required at fire time. Prevents sleeping over active work in another user session. Set 0 to disable the gate.

Logs go to ~/Library/Logs/mac-lock-sleep.log per user. Set LOCK_SLEEP_LOG to redirect, or LOCK_SLEEP_DRY_RUN=1 to log actions without sleeping (useful for testing).

Lock-screen settings compatibility #

mac-lock-sleep does not fight your System Settings → Lock Screen configuration — it rides on top of it. Whatever you set for "Require Password" and screensaver idle time controls when the screen locks; mac-lock-sleep starts its countdown from the com.apple.screenIsLocked notification, which fires only when the screen actually locks.

  • Explicit lock (Ctrl-Cmd-Q / Lock Screen menu) always fires the notification, independent of every setting. This is the primary trigger.
  • Idle auto-lock (display sleeps → require-password) also fires it, acting as a bonus safety net when you walk away and forget to lock. It cannot interrupt active work, because display sleep is vetoed by the same assertions (Chrome/Docker) while you're working — the display only sleeps when you're genuinely away.

If "Require Password" is off, idle display-sleep will not lock the screen, so only your explicit Ctrl-Cmd-Q triggers mac-lock-sleep. Both modes are compatible.

Multi-user behavior #

Every logged-in user runs an independent instance. Because the lock/unlock notifications are system-wide, any user locking the screen arms the countdown and any user unlocking cancels it. The 30-minute countdown restarts on each lock. Multiple instances firing at once is harmless — the second pmset sleepnow is a no-op on a machine already going to sleep.

Uninstall #

./uninstall.sh
rm -f ~/Library/Logs/mac-lock-sleep.log

Caveats #

  • After the machine sleeps and you wake it, the screen is still locked but mac-lock-sleep does not re-arm automatically — you woke it intentionally. Lock the screen again to start a fresh countdown.
  • pmset sleepnow bypasses idle-sleep assertions but not PreventSystemSleep. Keep that assertion count at 0 (see verification).
  • If an instance is killed and restarted by launchd after a lock event but before the timer fires, the in-flight countdown is lost; the next lock arms a fresh one.

Development / tests #

A self-contained dry-run test exercises the three paths (fire, HID-gate skip, unlock-cancel) without sleeping the machine:

swiftc -O src/mac-lock-sleep.swift -o mac-lock-sleep
swiftc -O test/test_harness.swift -o test/test_harness
./test/test_harness