Hook Scripts in AI
From the Claude Code cheat sheet ยท Hooks System ยท verified Jul 2026
Hook Scripts
Creating custom hook scripts
python
#!/usr/bin/env python3
# ~/.claude/hooks/log_commands.py
import sys
import json
from datetime import datetime
# Read input from stdin
data = json.loads(sys.stdin.read())
# Log command details
with open('command_log.txt', 'a') as f:
f.write(f"{datetime.now()}: {data['tool_input']['command']}\n")
# Return 0 to allow, non-zero to block
sys.exit(0)
# Make executable:
chmod +x ~/.claude/hooks/log_commands.py๐ Scripts receive JSON via stdin
๐ Return 0 to allow, non-zero to block
๐ Can modify or validate actions
hooksscripts