watch-hugo.sh 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/bin/bash
  2. # Hugo 文件监控守护进程
  3. # 监控 Obsidian 内容目录,自动重新编译
  4. HUGO_DIR="/home/zhn/.openclaw/workspace/skills/robdaily/deploy"
  5. CONTENT_DIR="/home/zhn/.openclaw/workspace/skills/robdaily/site/content"
  6. LOG_FILE="/tmp/hugo-watch.log"
  7. LOCK_FILE="/tmp/hugo-rebuild.lock"
  8. log() {
  9. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
  10. }
  11. rebuild() {
  12. # 防抖:如果 30 秒内已执行过,跳过
  13. if [ -f "$LOCK_FILE" ]; then
  14. last_run=$(stat -c %Y "$LOCK_FILE" 2>/dev/null || echo 0)
  15. current_time=$(date +%s)
  16. if [ $((current_time - last_run)) -lt 30 ]; then
  17. return
  18. fi
  19. fi
  20. log "开始重新编译 Hugo..."
  21. cd "$HUGO_DIR"
  22. docker compose exec -T hugo-personal-site hugo --minify 2>&1 | tail -5 >> "$LOG_FILE"
  23. touch "$LOCK_FILE"
  24. log "Hugo 重新编译完成"
  25. }
  26. # 监听内容目录
  27. log "开始监控:$CONTENT_DIR"
  28. inotifywait -m -r -e modify -e create -e delete --exclude '\.swp$' "$CONTENT_DIR" |
  29. while read -r directory event filename; do
  30. log "文件变化:$event $filename"
  31. rebuild
  32. done