เรียน claude
🎓
บทที่ 2 · ⏱ 10 นาที

Hooks — ทำงานอัตโนมัติก่อนหรือหลัง action

ผมตั้ง hook ป้องกันไม่ให้ Claude แก้ .env หรือ run rm — ทำงานได้เร็วโดยไม่ห่วงเรื่องพังของ

Hooks คือคำสั่งที่รันอัตโนมัติทุกครั้งที่เกิด event บางอย่างใน Claude Code ผมใช้เพราะมัน

  • ทำให้ workflow อัตโนมัติ — เช่นรัน Prettier ทุกครั้งที่ Claude แก้โค้ด
  • ป้องกันความเสียหาย — block การแก้ไฟล์สำคัญ
  • แจ้งเตือนเมื่องานยาวเสร็จ — Stop hook ส่ง notification

Event ที่ดักได้

Hookตอนใช้ทำ
UserPromptSubmitคนกด Enter ส่ง promptValidate input, log
PreToolUseก่อน Claude เรียก toolBlock, validate, modify
PostToolUseหลัง tool ทำงานFormat, lint, notify
StopSession จบExport, summary, notification
NotificationClaude ส่ง notificationCustom alert

Setup hook พื้นฐาน

แก้ ~/.claude/settings.json

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "npx prettier --write $CLAUDE_FILE_PATHS"
          }
        ]
      }
    ]
  }
}

แปลความ — ทุกครั้งที่ Claude ใช้ Edit/Write รัน Prettier กับไฟล์ที่แก้อัตโนมัติ

Hook ที่ผมใช้จริงในบริษัท

1) Auto format โค้ดหลังแก้

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "npx prettier --write $CLAUDE_FILE_PATHS" },
          { "type": "command", "command": "npx eslint --fix $CLAUDE_FILE_PATHS" }
        ]
      }
    ]
  }
}

2) Block ไม่ให้แก้ .env

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "if [[ \"$CLAUDE_FILE_PATHS\" =~ \\.env ]]; then echo 'BLOCKED — ห้ามแก้ .env' && exit 2; fi"
          }
        ]
      }
    ]
  }
}

ผลคือ Claude ขอแก้ .env hook block ทันที กันเผลอลืม

3) Notification เมื่อ session จบ

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"งาน Claude เสร็จแล้ว\" with title \"เสร็จ\"'"
          }
        ]
      }
    ]
  }
}

Mac ใช้ osascript / Windows ใช้ powershell BurntToastNotification

ผมใช้ตอนสั่งงานยาวๆ แล้วไปทำอย่างอื่น พอเสร็จเครื่องดังเตือน

4) Backup ก่อนลบไฟล์

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash(rm:*)",
        "hooks": [
          {
            "type": "command",
            "command": "mkdir -p /tmp/claude-backup && cp -r $CLAUDE_FILE_PATHS /tmp/claude-backup/ 2>/dev/null; true"
          }
        ]
      }
    ]
  }
}

ก่อน Claude รัน rm คัดลอกไฟล์ไปที่ /tmp/claude-backup/ ก่อน ถ้าลบผิดเอามาคืนได้

Environment Variables ที่ใช้ใน hook

Varค่า
$CLAUDE_FILE_PATHSไฟล์ที่ Edit คั่นด้วย space
$CLAUDE_SESSION_IDSession ID
$CLAUDE_TOOL_NAMEชื่อ tool ที่ถูกเรียก
$CLAUDE_TOOL_INPUTArgs ที่ส่งให้ tool (JSON)
$CLAUDE_PROJECT_DIRProject root

Exit code ที่สำคัญ

  • Exit 0 ผ่าน Claude ทำต่อได้
  • Exit 1 fail แต่ไม่ block แค่ warning
  • Exit 2 block Claude ไม่ทำ action นั้น

ใช้ exit 2 เป็นเครื่องมือ enforcement สำคัญ

Pro Tip — Hook สำหรับ policy

ตัวอย่าง — ห้ามมี console.log ใน src/

.claude/hooks/no-console-log.sh

#!/bin/bash
if [[ "$CLAUDE_FILE_PATHS" =~ src/ ]]; then
  if grep -l "console\.log" $CLAUDE_FILE_PATHS 2>/dev/null; then
    echo "BLOCKED — ห้ามมี console.log ใน src/"
    exit 2
  fi
fi
exit 0
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "bash .claude/hooks/no-console-log.sh" }
        ]
      }
    ]
  }
}

ของที่ต้องระวัง

  • Hook รันบนเครื่องคุณ — อย่าใส่ command ที่ดึงจากเน็ตมารัน อย่า trust hook ที่คน upload มา ตรวจก่อน
  • Hook ช้า = Claude ช้า — ถ้า hook รันหลายวินาที Claude ค้างรอ ใช้ & ให้ run background ถ้าไม่ต้อง block
  • Hook error อาจทำให้ Claude ค้าง — ทดสอบก่อนใช้จริง ตรวจ exit code ให้ถูก

ลองทำดู: ภารกิจ: ตั้ง hook แรก

บทนี้มีประโยชน์กับคุณมั้ยครับ?

ผมอ่าน feedback เองทุกอันแล้วเอาไปปรับเนื้อหา