Hooks are scripts that run automatically before or after Claude performs an action. They allow you to automate repetitive tasks and ensure consistent processes.
Hooks là các script chạy tự động trước hoặc sau khi Claude thực hiện một hành động. Chúng cho phép bạn tự động hóa các tác vụ lặp đi lặp lại và đảm bảo quy trình nhất quán.
Examples: run lint before each commit, send notification after deploy, or validate input before running dangerous commands.
Ví dụ: chạy lint trước mỗi commit, gửi notification sau khi deploy, hoặc validate input trước khi chạy lệnh nguy hiểm.
Runs before Claude uses a tool. Can block, modify, or add context to the action.
Chạy trước khi Claude sử dụng một tool. Có thể chặn, sửa đổi hoặc thêm context cho hành động.
1// .claude/hooks/pre-tool-use.js2module.exports = {3 // Run before any Bash command4 "Bash": async (params) => {5 // Block dangerous commands6 if (params.command.includes("rm -rf /")) {7 return { blocked: true, message: "Dangerous command blocked" };8 }9 // Add context10 return { context: "Remember to use --dry-run first" };11 }12};Runs after a tool completes. Can log results, send notifications, or trigger follow-up actions.
Chạy sau khi tool hoàn thành. Có thể log kết quả, gửi notification, hoặc trigger các hành động tiếp theo.
1// .claude/hooks/post-tool-use.js2module.exports = {3 // After any Write operation4 "Write": async (result) => {5 console.log(`File written: ${result.file_path}`);6 // Could send to logging service7 },89 // After commits10 "Bash": async (result, params) => {11 if (params.command.startsWith("git commit")) {12 // Notify team on Slack13 await sendSlackNotification("New commit created");14 }15 }16};Hooks are configured in Claude Code's settings.json file.
Hooks được cấu hình trong file settings.json của Claude Code.
1// ~/.claude/settings.json2{3 "hooks": {4 "preToolUse": [5 {6 "matcher": "Bash",7 "command": "node .claude/hooks/validate-bash.js"8 }9 ],10 "postToolUse": [11 {12 "matcher": "*",Claude Code can schedule tasks to run periodically within a session. This is useful for monitoring, reminders, or polling.
Claude Code có thể lên lịch các tác vụ chạy định kỳ trong phiên làm việc. Điều này hữu ích cho monitoring, reminders, hoặc polling.
1# Schedule a recurring check every 5 minutes2/loop 5m "check build status and notify if failed"34# One-shot reminder5"remind me in 30 minutes to check the deploy"67# Cron-style scheduling (session-only by default)8CronCreate: "*/5 * * * *" → "check CI status"910# Durable scheduling (survives restart)11CronCreate with durable: trueScheduled tasks chỉ chạy khi Claude session đang active. Durable tasks được lưu và chạy lại khi session restart.
- Auto-lint after each file write to ensure consistent code style.
Auto-lint sau mỗi file write để đảm bảo code style nhất quán.
- Validate environment before running deploy commands.
Validate environment trước khi chạy deploy commands.
- Send notification when task completes or error occurs.
Gửi notification khi task hoàn thành hoặc lỗi xảy ra.
- Auto-backup before making large changes.
Auto-backup trước khi thực hiện thay đổi lớn.
- Auto-inject context based on working file.
Inject context tự động dựa trên file đang làm việc.
Some special keywords in prompts automatically trigger skills or behaviors.
Một số từ khóa đặc biệt trong prompt tự động trigger các skills hoặc behaviors.
1# Magic keywords trigger skills2"autopilot ..." → /oh-my-claudecode:autopilot3"ralph ..." → /oh-my-claudecode:ralph4"ulw ..." → /oh-my-claudecode:ultrawork5"deep interview..." → /oh-my-claudecode:deep-interview67# Context injection via hooks8[MAGIC KEYWORD: AUTOPILOT] → skill gets loadedKey Takeaways
Điểm Chính
- Hooks run automatically before/after Claude actionsHooks chạy tự động trước/sau các hành động của Claude
- PreToolUse can block or modify actionsPreToolUse có thể chặn hoặc sửa đổi hành động
- PostToolUse can log, notify, or trigger follow-upsPostToolUse có thể log, thông báo hoặc trigger tiếp theo
- Scheduled tasks can run periodically during sessionScheduled tasks có thể chạy định kỳ trong session
Practice
Test your understanding of this chapter
When does a PreToolUse hook run?
PreToolUse hook chạy khi nào?
Scheduled tasks continue running after the Claude session ends.
Scheduled tasks tiếp tục chạy sau khi Claude session kết thúc.
Complete the hook type that runs after tool completion
Hoàn thành loại hook chạy sau khi tool hoàn thành
ToolUseWhat can a PreToolUse hook return to prevent an action?
PreToolUse hook có thể return gì để ngăn hành động?