Claude Code hook not working: why a PreToolUse hook doesn't fire or doesn't block
Tested on Claude Code 2.1.332: a PreToolUse hook is skipped when the matcher is not the exact tool name, the script is not executable or Claude Code starts in a subfolder, and exit 1, a timeout or a top-level permissionDecision let the command run. The fixes, and a hook that fails closed.
· 7 min read
A Claude Code PreToolUse hook that “doesn’t work” is usually one of two problems. Either it never runs, because the matcher does not name the tool exactly, the script cannot start, or Claude Code was started somewhere it does not read the settings from. Or it runs and fails in a way Claude Code treats as a yes: exit code 1, a timeout, JSON it cannot read, or a decision in the wrong place.
We ran 33 cases on Claude Code 2.1.332 with a real model on 27 November 2026, each in a fresh folder, asking Claude to run touch marker.txt. In 31 of them the hook was meant to stop the command, and in 15 of those the file appeared anyway. Here is each case, what fixed it, and a gate that refuses when it breaks.
Checklist
- Match the tool name exactly.
Bash, notbashorBas. A matcher cannot look at the command:Bash(git push:*)matches nothing. Check the command inside the script, or use"*"or leave the matcher out to match every tool. - Start Claude Code in the folder that holds .claude/. Started from a subfolder, our project hook was not loaded at all.
- Make the script executable, and use an absolute path or
"$CLAUDE_PROJECT_DIR"/.... A script withoutchmod +xand a wrong path both let the command run. - Refuse with exit 2 and the reason on stderr. Exit 1 is a non-blocking error: the command ran. With exit 2, a reason on stdout reached the model as an error with no message.
- Put a JSON decision inside
hookSpecificOutput. A top-levelpermissionDecisionwas ignored. Print one JSON object and nothing else. - Add
"onFailure": "block"(v2.1.295 or later). With it, exit 1, a timeout and a missing script each blocked. - Never put
"async": trueon a gate, and look for"disableAllHooks": truein every settings file, including your user and local ones.
The hook never ran
| Option | Command ran | What happened |
|---|---|---|
| Matcher "bash", lower case | Command ranYes | What happenedThe hook was not called. Tool names are case-sensitive: the tool is Bash. |
| Matcher "Bas" | Command ranYes | What happenedNot called. A matcher of plain letters is compared as a whole name, not a prefix. |
| Matcher "Bash(touch:*)", written like a permission rule | Command ranYes | What happenedNot called. A matcher names the tool only; it cannot look at the command. |
| Claude Code started from a subfolder of the project | Command ranYes | What happenedNot called, with the hook in the parent folder's .claude/settings.json or settings.local.json, and with an absolute script path too. |
| Script is not executable (no chmod +x) | Command ranYes | What happenedThe hook could not start. Nothing told the model. |
| Script path does not exist | Command ranYes | What happenedThe hook could not start. Nothing told the model. |
| "disableAllHooks": true in the same settings | Command ranYes | What happenedNot called. |
Claude Code’s documentation explains the matcher results: a matcher of only letters, digits, _, -, spaces, commas and | is a list of exact tool names, anything else is an unanchored regular expression, and "*", "" or no matcher matches every tool. Bash|Write, "*", an empty matcher and no matcher all called our hook. For MCP tools the documentation says mcp__memory matches no tool and mcp__memory__.* is needed; we did not test that one.
The subfolder case is easy to hit in a monorepo. Our runs from sub/ looked for sub/.claude/settings.json and never ran the hook in the parent’s .claude/, even after a git commit and with an absolute script path. If you start Claude Code in package folders, put the gate in your user settings or in each folder you start from.
The hook ran, and the command went ahead
| Option | Command ran | What happened |
|---|---|---|
| Exit 1, message on stderr | Command ranYes | What happenedNothing reached the model; it reported the command done. |
| Sleeps past a 3 second timeout, then exits 2 | Command ranYes | What happenedThe call went ahead once the hook was cut off. |
| Prints a deny as JSON that does not parse, exit 0 | Command ranYes | What happenedTreated as a failed hook, so the call went ahead. |
| {"permissionDecision": "deny"} at the top level, without hookSpecificOutput | Command ranYes | What happenedIgnored: the decision has to be inside hookSpecificOutput. |
| "async": true on a hook that exits 2 | Command ranYes | What happenedA background hook cannot hold the call back. |
Exit 1 is the trap: it is the usual Unix failure code, so a script that hits an error, or uses exit 1 to say no, lets the call through. The documentation says the same, and that a timed-out PreToolUse hook does not block either: the call carries on through the normal permission flow. The default timeout for a command hook is 600 seconds.
What did block
| Option | Command ran | What happened |
|---|---|---|
| Exit 2, reason on stderr | Command ranNo, blocked | What happenedBlocked. The model quoted the reason and did not retry. |
| Exit 2, reason on stdout only | Command ranNo, blocked | What happenedBlocked, but the model saw an error with no message. |
| hookSpecificOutput with permissionDecision "deny", exit 0 | Command ranNo, blocked | What happenedBlocked, with the permissionDecisionReason shown to the model. |
| The same JSON deny, but exit 1 | Command ranNo, blocked | What happenedBlocked: with valid JSON, the fields decide and the exit code is ignored. |
| {"decision": "block"} at the top level, the old shape | Command ranNo, blocked | What happenedBlocked. The docs call it deprecated for PreToolUse; "block" maps to "deny". |
| Exit 1, timeout or missing script, with "onFailure": "block" | Command ranNo, blocked | What happenedBlocked in all three, and the model was told the hook failed. |
| Exit 2 in a run with --dangerously-skip-permissions | Command ranNo, blocked | What happenedBlocked: hooks still run and still refuse in bypass mode. |
| Hook in .claude/settings.local.json instead of settings.json | Command ranNo, blocked | What happenedBlocked, the same as from settings.json. |
The JSON shape that worked, printed on stdout with exit 0:
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "Deploys need a person"
}
}What “allow” and “ask” do
- A hook’s allow skips the permission prompt. With no allow rule, the same command waited for approval; with the hook answering
"allow", it ran. A hook that answers allow by default approves everything it matches. - Deny rules still win. With
--disallowedTools 'Bash(touch:*)', a hook’s allow did not let the command run. - Ask is a no in claude -p. Nobody can answer the prompt, so the call was refused, even though an allow rule covered it.
In a folder Claude Code has not been trusted in, it printed “Ignoring 2 permissions.allow entries from .claude/settings.json: this workspace has not been trusted”, but the project’s hooks still ran and blocked. Running with --dangerously-skip-permissions did not turn hooks off either.
A hook that refuses when it breaks
Set onFailure to block and a timeout you choose, then write the script so every error is an exit 2:
// .claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/gate.sh",
"timeout": 30,
"onFailure": "block"
}
]
}
]
}
}#!/bin/sh
# gate.sh: every way out is a refusal unless the check says yes.
# chmod +x .claude/hooks/gate.sh, or it never starts.
input=$(cat) || { echo "gate: could not read the call" >&2; exit 2; }
# my-check stands for your own check; give it a deadline of its own,
# well inside the 30 second timeout above.
answer=$(printf '%s' "$input" | /usr/local/bin/my-check) \
|| { echo "gate: check failed" >&2; exit 2; }
[ "$answer" = "allow" ] && exit 0
echo "gate: not allowed ($answer)" >&2 # stderr, not stdout
exit 2The docs say direct edits to hooks in settings files are normally picked up by a file watcher. Each claude -p run reads the settings fresh; in a long interactive session, check /hooks after an edit.
Test it yourself
Run this with the hook in place. Then break the hook on purpose (exit 1, a long sleep, a typo in the path) and run it again: with onFailure: "block", marker.txt should never appear.
# In the folder that holds .claude/, not a subfolder.
claude -p "Use the Bash tool to run exactly this command once: touch marker.txt" \
--allowedTools 'Bash(touch:*)' --debug-file hook-debug.txt
ls marker.txt # exists: the hook did not block
grep "Hooks: PreToolUse" hook-debug.txt # nothing: the hook never ranWhere VariantGate fits
A hook can only say yes or no from what the script knows. variantgate hook claude-code sends the call to a person instead: the command waits in the VariantGate inbox, on a phone or in Slack, and runs only after someone approves it. It waits two minutes by default, then takes the request back and denies the call; it answers with a refusal rather than an ask. It cannot help when the hook never starts, so install it rather than running it through npx, and add "onFailure": "block". Setup is in the Claude Code guide.
For the same failure tests across seven agents, see coding agent hooks that fail open; for Codex, Codex hook failed. When an allow or deny rule is the problem rather than a hook, see Claude Code permissions not working. To check a settings file before you rely on it, paste it into the Claude Code hooks checker or the settings checker.
Sources
Give your agents somewhere to ask.
Free for 100 requests a month. Sign in with your email and have a first request waiting a few minutes from now.
Start free