From 385bb37fa050c2078ae0c877c60b160130e8715c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=93=99=F0=9D=93=BE=F0=9D=93=B5=F0=9D=93=B2?= =?UTF-8?q?=F0=9D=93=B8?= Date: Mon, 27 Jan 2025 01:07:33 +0100 Subject: [PATCH] Add inotify/opt/nginx_change_monitor.sh --- inotify/opt/nginx_change_monitor.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 inotify/opt/nginx_change_monitor.sh diff --git a/inotify/opt/nginx_change_monitor.sh b/inotify/opt/nginx_change_monitor.sh new file mode 100644 index 0000000..d8782e8 --- /dev/null +++ b/inotify/opt/nginx_change_monitor.sh @@ -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 \ No newline at end of file