Created
August 17, 2026 12:05
-
-
Save dui/b68902f230b4ccaa703cd817463e509d to your computer and use it in GitHub Desktop.
Keep a clamshell Mac awake reliably: caffeinate -is LaunchAgent + toggle script + 97-line Swift menu bar app that shows the live pmset assertion state (truth, not config)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # awake — toggle the keep-awake LaunchAgent (fi.dui.keepawake, caffeinate -is). | |
| # The -s (PreventSystemSleep) is what survives monitor-pull with the lid closed | |
| # on AC (A/B isolated 2026-08-15; idle-class apps lose that pull 4/4). | |
| # AC-only by design: on battery the Mac sleeps normally regardless. | |
| # | |
| # awake show status (assertion truth, not config) | |
| # awake on load the agent, verify the assertion appears | |
| # awake off unload the agent, verify the assertion is gone | |
| set -u | |
| PLIST="$HOME/Library/LaunchAgents/fi.dui.keepawake.plist" | |
| LABEL="fi.dui.keepawake" | |
| UID_N=$(id -u) | |
| held() { pmset -g assertions | grep -q "PreventSystemSleep named: \"caffeinate"; } | |
| status() { | |
| if held; then | |
| pid=$(launchctl print "gui/$UID_N/$LABEL" 2>/dev/null | awk '/pid =/{print $3}') | |
| echo "AWAKE — PreventSystemSleep held (agent pid ${pid:-?}). Survives monitor-pull, lid closed, on AC." | |
| else | |
| echo "SLEEPY — no PreventSystemSleep assertion. Monitor-pull with lid closed will sleep the Mac." | |
| fi | |
| } | |
| case "${1:-status}" in | |
| on) | |
| launchctl bootstrap "gui/$UID_N" "$PLIST" 2>/dev/null | |
| sleep 1 | |
| held && status || { echo "FAILED: agent loaded but no assertion appeared — check: launchctl print gui/$UID_N/$LABEL"; exit 1; } | |
| ;; | |
| off) | |
| launchctl bootout "gui/$UID_N/$LABEL" 2>/dev/null | |
| sleep 1 | |
| held && { echo "FAILED: assertion still present after unload"; exit 1; } || status | |
| ;; | |
| status|*) | |
| status | |
| ;; | |
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
| <plist version="1.0"> | |
| <dict> | |
| <key>Label</key> | |
| <string>fi.dui.keepawake</string> | |
| <key>ProgramArguments</key> | |
| <array> | |
| <string>/usr/bin/caffeinate</string> | |
| <string>-is</string> | |
| </array> | |
| <key>RunAtLoad</key> | |
| <true/> | |
| <key>KeepAlive</key> | |
| <true/> | |
| </dict> | |
| </plist> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment