Skip to content

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.

The hook runs one command:

commandlatch done --notify --lock-sleep-after 5m

Which means:

  1. A banner — “Locking in 5m” — appears on your Mac right away.
  2. The Mac is scheduled to lock + sleep in 5 minutes.
  3. 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.

In the menu-bar app, open Settings → Command-line tool → Install commandlatch on PATH and approve the admin prompt. Then confirm it’s working:

Terminal window
commandlatch status

It should print your paired device. If it says No config…, finish pairing first. Full details: Install → command-line tool.

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"
}
]
}
]
}
}
  1. In a terminal, cd into any project and run claude.
  2. Ask it to do something small — e.g. print the current directory.
  3. When Claude finishes, a “Locking in 5m” banner appears on your paired Mac within a couple of seconds.
  4. Either wait 5 minutes (the Mac locks and sleeps) or cancel it.

A pending lock can be cancelled any of these ways — they all do the same thing:

  • Run commandlatch done --cancel in any terminal.
  • Click Cancel Pending Action in the menu-bar menu (it shows the countdown).
  • Click Cancel on the countdown pill in the dashboard.

The command’s flags compose — adjust the hook to taste:

CommandBehaviour
commandlatch doneLock + sleep immediately.
commandlatch done --notifyBanner, then lock now.
commandlatch done --lock-sleep-after 10mLock in 10 minutes (no banner).
commandlatch done --notify --lock-sleep-after 5mBanner 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).

  • 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 found in the hook log. Use the full path in settings.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.

The commandlatch run wrapper keeps your Mac awake during a long job and locks it after, propagating the job’s exit code:

Terminal window
commandlatch run --ttl 2h --lock-sleep-after 5m -- pnpm test:e2e

See the commandlatch CLI for awake, run, and the rest.