Skip to content

inotify-tools

监控nginx配置

#!/bin/bash

NGINX_DIR="/etc/nginx"
LOG_FILE="/var/log/nginx_git_watch.log"

cd $NGINX_DIR || exit 1

inotifywait -m -r \
  -e modify,create,delete,move \
  --exclude '\.swp$|\.tmp$|\.log$|~$' \
  $NGINX_DIR |
while read path action file; do
    echo "$(date) - $action - $path$file" >> $LOG_FILE

    # 防止频繁提交(1 秒内合并)
    sleep 1

    # 有变更才提交
    if ! git diff --quiet; then
        git add .
        git commit -m "Auto update nginx config: $action $file"
        git push origin main
    fi
done
☁️ 部署建议
如果你打算长期运行项目(博客 / API / 自动化脚本),建议直接用云服务器,会比本地稳定很多。
👉 查看云服务器(新用户优惠)