#!/bin/bash # Directory to monitor TARGET_DIR="/etc/nginx/live/" # Log file for tracking changes LOG_FILE="/var/log/nginx_change_monitor.log" # Function to check Nginx configuration and reload if valid reload_nginx_if_valid() { echo "Checking Nginx configuration..." | tee -a "$LOG_FILE" if nginx -t &>> "$LOG_FILE"; then echo "Nginx configuration test successful. Reloading Nginx..." | tee -a "$LOG_FILE" nginx -s reload else echo "Nginx configuration test failed. No changes applied." | tee -a "$LOG_FILE" fi } # Monitor the directory for changes using inotifywait echo "Starting Nginx change monitor for directory: $TARGET_DIR" | tee -a "$LOG_FILE" inotifywait -m -r -e modify,create,delete --format '%w%f %e' "$TARGET_DIR" | while read FILE EVENT do echo "Change detected in $FILE ($EVENT) at $(date)" | tee -a "$LOG_FILE" reload_nginx_if_valid done