Content
1#!/usr/bin/env bash
2# Hook type: Stop / PostToolUse
3# Trigger: Bash tool calls that complete after a long duration
4#
5# Place this in your hooks config:
6# hooks:
7# PostToolUse:
8# - matcher: "Bash"
9# hooks:
10# - type: command
11# command: /path/to/notify-on-long-run.sh
12
13set -euo pipefail
14
15# Read JSON from stdin
16INPUT=$(cat)
17TOOL=$(echo "$INPUT" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('tool_name',''))" 2>/dev/null || true)
18
19if [ "$TOOL" != "Bash" ]; then
20 exit 0
21fi
22
23DURATION=$(echo "$INPUT" | python3 -c "
24import sys, json
25d = json.load(sys.stdin)
26print(d.get('duration_ms', 0))
27" 2>/dev/null || echo "0")
28
29THRESHOLD=30000 # 30 seconds in ms
30
31if [ "$DURATION" -lt "$THRESHOLD" ]; then
32 exit 0
33fi
34
35CMD=$(echo "$INPUT" | python3 -c "
36import sys, json
37d = json.load(sys.stdin)
38cmd = d.get('tool_input', {}).get('command', 'unknown command')
39print(cmd[:80])
40" 2>/dev/null || echo "command")
41
42SECONDS_TAKEN=$(( DURATION / 1000 ))
43MESSAGE="Finished in ${SECONDS_TAKEN}s: $CMD"
44
45# macOS
46if command -v osascript &>/dev/null; then
47 osascript -e "display notification \"$MESSAGE\" with title \"Claude Code\""
48# Linux (notify-send)
49elif command -v notify-send &>/dev/null; then
50 notify-send "Claude Code" "$MESSAGE"
51fi
52