Claude Code Hooks Not Firing: Which Mistakes Fail Silently

If the line under the tool call reads PreToolUse:Bash hook error: followed by the reason your script printed, and the command didn’t run, the hook did its job. PreToolUse:Bash hook failed means the guard broke and the command already ran.

Claude Code runs the command unless your hook hands back a clear “no” in a form it reads, such as exit 2 or a JSON deny decision; a guard that breaks lets the command through. /hooks showed something wrong only for the file, event-name and JSON mistakes; the rest looked exactly like a working guard.

Grid of seven hook mistakes (Bad path or no +x, exit 1, Event name typo, Broken JSON, Wrong file, Lowercase matcher, Wrong answer shape) against three places to look (/hooks, Interactive, claude -p); blue cells read "listed" or "nothing", gray cells read "hook failed", "no hooks yet", "Warning", "Error" or "stream-json only"
Every one of these ran cat .env; the blue cells are the places that told you nothing.

Data (CSV)CC BY 4.0

When a guard isn’t firing or isn’t blocking, start from the screen you already looked at:

  • hook failed under the tool call: the shell error below it names the fix.
  • no hooks yet in /hooks: the settings file, the event name or the JSON.
  • Listed in /hooks, no message anywhere: log the script’s first line.
  • Only claude -p output: run the hook by hand.
On this page
  1. “PreToolUse:Bash hook failed”: fix the path, the execute bit or the exit code
  2. “no hooks yet” in /hooks: the file, the event name or the JSON
  3. Listed in /hooks, no message: log the first line
  4. Under claude -p, hooks run but every warning disappears
  5. A guard holds only once you’ve watched it block a fake .env

“PreToolUse:Bash hook failed”: fix the path, the execute bit or the exit code

hook failed is Claude Code reporting that it got no answer from your hook and went ahead without one:

Claude Code terminal showing "PreToolUse:Bash hook failed", "/bin/sh: 1: .../.claude/hooks/gaurd.sh: not found" and "Exit 127, likely a missing program: The check may not have run. See /hooks.", followed by the output of cat .env: FAKE_KEY=test-not-real
The guard never started, and the fake key printed right under hook failed (2.1.327).

The shell error printed under it names which one:

  • not found: the path is wrong. Write it as "$CLAUDE_PROJECT_DIR"/.claude/hooks/guard.sh. A relative ./.claude/hooks/guard.sh can break once Claude has cd‘d into another folder.
  • Permission denied: the script isn’t executable. Run chmod +x on it.
  • Your own block message: the script ran and exited 1. Only exit 2 blocks a PreToolUse call; Claude Code reports exit 1 and carries on.

“no hooks yet” in /hooks: the file, the event name or the JSON

/hooks lists what Claude Code actually loaded, so no hooks yet under PreToolUse means your file was never read or was thrown away whole. In the second case one trailing comma switches off every hook in that file, not just the one you were editing.

Claude Code reads hooks from three settings files:

  • .claude/settings.json
  • .claude/settings.local.json
  • ~/.claude/settings.json

An event typo or broken JSON shows in an interactive session only, in a dialog that opens before your first prompt; a hook in the wrong file shows nothing. Pick Continue and it’s gone; quit and start claude again in that folder to see it once more:

Mistake At startup Fix
Hook in .claude/hooks.json or a settings.json at the repo root No warning; it loads nowhere Move it into one of the three files
Event typo like PreToolUSE Settings Warning Spell the event PreToolUse
Broken JSON Settings Error: “Files with errors are skipped entirely” Fix the JSON

Broken JSON includes a handler placed straight under the event. The event holds matcher groups, and each group holds its own hooks list:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard.sh" }
        ]
      }
    ]
  }
}

You don’t need to restart after fixing it: in my test session, the next cat .env after the edit was already blocked.

Listed in /hooks, no message: log the first line

A hook in /hooks is registered, and that’s all it proves. Two steps come after registration and leave no trace when they fail: whether the matcher catches this tool’s name, and whether the script’s answer is a “no” Claude Code reads.

One log line, right under the shebang, tells them apart. Here it is in a minimal guard, built like the one I tested every case with (it needs jq):

#!/bin/bash
echo "$(date +%T) fired" >> /tmp/guard-hook.log
cmd=$(jq -r '.tool_input.command // ""')
if [[ "$cmd" == *.env* ]]; then
  echo "blocked: .env read not allowed" >&2
  exit 2
fi
exit 0

Keep the >&2. With exit 2, Claude Code takes the block reason from stderr; a message echoed to stdout still blocks, but Claude has nothing to quote.

Ask Claude for one command the matcher should catch, then run cat /tmp/guard-hook.log.

No log line: the matcher doesn’t match the tool name

A matcher made of plain names has to match the tool name exactly, case included, so "bash" never fires for the Bash tool. That was my first .env guard: cat .env went straight through, yet the script exited 2 with the right message when I piped a sample in by hand.

/hooks lists it as bash · command, just like a working guard, so I kept debugging the script; only the log file that never appeared pointed me at the matcher.

Write it "Bash"; for several tools, separate the names with | or a comma, as in "Bash|Read". Any other character, such as *, turns the matcher into a regular expression.

Log written, command ran: the answer wasn’t a “no” Claude Code reads

The hook ran, so the fault is in what it said back:

  • No shebang. The script runs under /bin/sh, which on my machine stopped at [[ with not found and fell through to exit 0. Put #!/bin/bash above the log line.
  • A guard on PostToolUse. It runs after the tool, when the output has already reached the model. Move it to PreToolUse.
  • Slower than its timeout. The tool call shows running PreToolUse hook with a seconds counter, then the command runs with no message once time is up. Make the script faster, or raise timeout, in seconds, on the same handler as command.
  • A deny at the top level. permissionDecision only counts inside hookSpecificOutput; at the top level it’s ignored without an error. This form, with exit 0, blocks:
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"no .env"}}

Under claude -p, hooks run but every warning disappears

Hooks do run under claude -p, and a working guard blocks there too. What -p drops is the messages: no Settings Warning, no Settings Error, no hook failed. So a silent CI run can hide any row of the grid, a wrong path included.

For CI, add --output-format stream-json --verbose. A hook that couldn’t start or ran out of time then leaves a system event tagged hook_no_verdict, and a step can fail the job when grep -q hook_no_verdict finds it. exit 1 and the other rows leave no event.

To see which mistake you have, run the command from your settings file by hand, from the repo root:

export CLAUDE_PROJECT_DIR="$PWD"
echo '{"tool_name":"Bash","tool_input":{"command":"cat .env"}}' | "$CLAUDE_PROJECT_DIR"/.claude/hooks/guard.sh; echo "exit $?"

A shell error or exit 1 sends you to the hook failed fixes. exit 2 with your message, from a script that starts with #!/bin/bash, means the script is fine and the settings file is at fault: open the folder interactively once and check /hooks.

A guard holds only once you’ve watched it block a fake .env

Whichever row you came from, neither /hooks nor a setting tells you the fix worked; only seeing the guard stop a command does.

Why “onFailure”: “block” doesn’t prove it

"onFailure": "block", set on the handler next to command, turns a failed hook into a refusal. It can only act when Claude Code knows something failed:

Flow from Tool call down through Matcher fits?, Script starts?, Answers in time? and Clear no? to Blocked; under the heading "With onFailure: block", a "no" at Script starts? or Answers in time? and an "exit 1" at Clear no? end in Blocked, while a "no" at Matcher fits? and an "exit 0" at Clear no? end in blue "Still runs" boxes
onFailure blocks a hook that fails, not one that never matched or answered wrong.

When my own guard broke with exit 1 under the option, every command stopped, a plain git status included, with No stderr output under the block message and no hint of why. The lowercase matcher I’d actually tripped on gets past the option anyway, so I left it out.

The fake .env test

  1. Put a .env holding a fake value such as FAKE_KEY=test-not-real in the repo and ask Claude to run cat .env.
  2. Check that Claude quotes your block message and stops instead of finding another way to read the file. If the key prints, go back to the grid with what you saw this time.
  3. Ask for one ordinary command with no .env in it, such as git status, and check that it runs.

Sources

Related posts