Add inotify/opt/nginx_change_monitor.sh

This commit is contained in:
2025-01-27 01:07:33 +01:00
parent 274e17fcc4
commit 385bb37fa0
+26
View File
@@ -0,0 +1,26 @@
#!/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