Skip to content
DocsUse AIadvancedHooks & Automation
Chapter 13 of 15·advanced·10 min read

Hooks & Automation

Hooks & Tự Động Hóa

Automating workflows with hooks and scheduled tasks

Hover or tap any paragraph to see Vietnamese translation

What are Hooks?

Hooks are scripts that run automatically before or after Claude performs an action. They allow you to automate repetitive tasks and ensure consistent processes.

Examples: run lint before each commit, send notification after deploy, or validate input before running dangerous commands.

Types of Hooks

PreToolUse Hooks

Runs before Claude uses a tool. Can block, modify, or add context to the action.

PreToolUse Hook
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};

PostToolUse Hooks

Runs after a tool completes. Can log results, send notifications, or trigger follow-up actions.

PostToolUse Hook
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};

Configuring Hooks

Hooks are configured in Claude Code's settings.json file.

settings.json
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": "*",

Scheduled Tasks

Claude Code can schedule tasks to run periodically within a session. This is useful for monitoring, reminders, or polling.

Scheduled Tasks
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: true
Info
Scheduled tasks only run when the Claude session is active. Durable tasks are saved and run again when session restarts.

Common Hook Use Cases

  • Auto-lint after each file write to ensure consistent code style.
  • Validate environment before running deploy commands.
  • Send notification when task completes or error occurs.
  • Auto-backup before making large changes.
  • Auto-inject context based on working file.

Magic Keywords

Some special keywords in prompts automatically trigger skills or behaviors.

Magic Keywords
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 loaded

Key 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

Quiz

When does a PreToolUse hook run?

PreToolUse hook chạy khi nào?

True or False

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.

Code Challenge

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

ToolUse
Quiz

What can a PreToolUse hook return to prevent an action?

PreToolUse hook có thể return gì để ngăn hành động?

← → to navigate chapters
Built: 4/8/2026, 12:01:11 PM