Auto-lock with Claude Code
Start a long Claude Code task, walk away, and a few minutes after Claude finishes your Mac warns you and then locks and sleeps itself. If you come back in time, one command cancels it.
It works by adding a Stop hook to Claude Code that runs the commandlatch
command when Claude finishes responding.
What it does
Section titled “What it does”The hook runs one command:
commandlatch done --notify --lock-sleep-after 5mWhich means:
- A banner — “Locking in 5m” — appears on your Mac right away.
- The Mac is scheduled to lock + sleep in 5 minutes.
- If you’re still around, cancel it before it fires (see below).
The command returns instantly, so it never blocks your next prompt — the 5-minute grace period is held on the Mac, not in the terminal.
1. Install the commandlatch command
Section titled “1. Install the commandlatch command”In the menu-bar app, open Settings → Command-line tool → Install commandlatch on PATH and approve the admin prompt. Then confirm it’s working:
commandlatch statusIt should print your paired device. If it says No config…, finish
pairing first. Full details:
Install → command-line tool.
2. Add the Stop hook
Section titled “2. Add the Stop hook”Add this to ~/.claude/settings.json to enable it everywhere (or to a
project’s .claude/settings.json to scope it to one repo):
{ "hooks": { "Stop": [ { "hooks": [ { "type": "command", "command": "commandlatch done --notify --lock-sleep-after 5m" } ] } ] }}3. Try it
Section titled “3. Try it”- In a terminal,
cdinto any project and runclaude. - Ask it to do something small — e.g. print the current directory.
- When Claude finishes, a “Locking in 5m” banner appears on your paired Mac within a couple of seconds.
- Either wait 5 minutes (the Mac locks and sleeps) or cancel it.
Cancelling
Section titled “Cancelling”A pending lock can be cancelled any of these ways — they all do the same thing:
- Run
commandlatch done --cancelin any terminal. - Click Cancel Pending Action in the menu-bar menu (it shows the countdown).
- Click Cancel on the countdown pill in the dashboard.
Tweaking it
Section titled “Tweaking it”The command’s flags compose — adjust the hook to taste:
| Command | Behaviour |
|---|---|
commandlatch done | Lock + sleep immediately. |
commandlatch done --notify | Banner, then lock now. |
commandlatch done --lock-sleep-after 10m | Lock in 10 minutes (no banner). |
commandlatch done --notify --lock-sleep-after 5m | Banner now, lock in 5m (the form above). |
Want a 10-minute grace period? Change 5m to 10m. Durations accept s/m/h
(e.g. 30s, 5m, 1h).
If it doesn’t work
Section titled “If it doesn’t work”- No banner. Check the Mac is Online (
commandlatch status/ the dashboard) and that notifications are allowed — in the menu bar, Settings → Security checks → Turn on notifications. Do-Not-Disturb / Focus also silences banners. commandlatch: command not foundin the hook log. Use the full path insettings.json:/usr/local/bin/commandlatch.- Locks immediately instead of waiting. A glued-together flag like
--lock-sleep-after5m(no space) is rejected. Use a space or=:--lock-sleep-after=5m.
More: the commandlatch CLI and
Troubleshooting.
Beyond the Stop hook
Section titled “Beyond the Stop hook”The commandlatch run wrapper keeps your Mac awake during a long job and
locks it after, propagating the job’s exit code:
commandlatch run --ttl 2h --lock-sleep-after 5m -- pnpm test:e2eSee the commandlatch CLI for awake, run, and the rest.
See also
Section titled “See also”- The
commandlatchCLI — every flag the hook can use. - Use the menu-bar app — cancel a pending lock on the Mac.
- Troubleshooting — when a hook-scheduled lock doesn’t fire.