DEV Community

Aermin Huang
Aermin Huang

Posted on

Building LidGuard: a safer way to keep your Mac running with the lid closed

A developer carrying a partially open laptop on the street so a coding task can keep running

Ever carried your Mac like this just to keep a coding task alive?

Sometimes I need to leave with my MacBook closed while a long Codex task, build, download, or phone-based remote-control session is still running. By default, closing the lid puts macOS to sleep and interrupts all of that work.

I built LidGuard, an MIT-licensed SwiftUI menu bar app, to make this a deliberate and reversible choice instead of a hidden global setting.

Two explicit modes

LidGuard keeps the product model intentionally small:

  • Keep Running lets the Mac continue working after the lid closes.
  • Normal Sleep immediately returns lid-close behavior to macOS.

Keep Running

LidGuard Keep Running mode in the macOS menu bar

Normal Sleep

LidGuard Normal Sleep mode in the macOS menu bar

I deliberately did not install Codex or other agent hooks. An agent starting or finishing should not silently change a machine-wide power setting. The user starts a LidGuard session explicitly and can see its current state in the menu bar.

That also makes the app useful beyond AI agents: remote access from a phone, long builds, downloads, data processing, and other background work all fit the same model.

Recovery safeguards

The difficult part was not keeping the Mac awake. It was making sure the setting did not remain enabled accidentally.

LidGuard timer, low-battery threshold, and thermal safeguard controls

LidGuard provides three safeguard profiles:

  • Strict requires a deadline and restores normal sleep at 30% battery or serious thermal pressure.
  • Balanced supports timed or unlimited sessions and an adjustable 10%-50% battery threshold.
  • Manual gives more control, but critical thermal pressure always restores normal sleep.

Timed sessions can run for up to seven days, and the app warns five minutes before a deadline. Battery protection applies only while discharging, so connecting power does not unexpectedly end a session.

A narrow implementation

The menu bar UI is unprivileged. A limited root helper performs only structured start, stop, update, and status operations. It does not accept arbitrary shell commands or file paths.

Internally, LidGuard changes only the system's SleepDisabled state and verifies the result after every change:

sudo pmset -a disablesleep 1  # Keep running
sudo pmset -a disablesleep 0  # Restore normal sleep
Enter fullscreen mode Exit fullscreen mode

It does not create a virtual display, capture the screen, change sleep or displaysleep, or add an extra caffeinate assertion. If another tool changes the setting, LidGuard does not continually fight it.

The helper persists the session deadline and safeguards, so protection continues even if the menu bar app exits or the helper restarts.

Try the preview

The current preview supports Apple Silicon Macs running macOS 13 or later. It includes the menu bar app, privileged helper, and a lidguard CLI.

Because I am distributing this without a paid Developer ID certificate, the build is ad-hoc signed. The README documents the one-time System Settings → Privacy & Security → Open Anyway step. After the helper is installed once, normal mode switching does not require another administrator password.

I would especially appreciate feedback on the safety defaults, remote-control compatibility, and the explicit-session model. If LidGuard is useful to you, please consider giving the repository a ⭐️ on GitHub so more people can discover it.

Top comments (0)