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

More AI tasks

Back to the full Claude Code cheat sheet