11#! /bin/bash
2- set -euo pipefail
2+ set -uo pipefail
33
44# script to watch changes in WATCH_DIR and reload nginx on .conf file changes
55
66WATCH_DIR=" ${WATCH_DIR:-/ etc/ nginx} "
77RELOADER_LOG=" ${RELOADER_LOG:-/ var/ log/ nginx/ reloader.log} "
88RELOADER_DELAY=" ${RELOADER_DELAY:- 2} "
9- FAIL_COUNT=0
10- MAX_FAILS=5
9+ LAST_HASH=" "
1110
1211log () {
1312 local msg=" [$( date ' +%Y-%m-%d %H:%M:%S' ) ] $1 "
@@ -20,6 +19,11 @@ error_exit() {
2019 exit 1
2120}
2221
22+ # compute hash of all .conf files
23+ compute_hash () {
24+ find " ${WATCH_DIR} " -name ' *.conf' -type f -exec md5sum {} \; 2> /dev/null | sort | md5sum | awk ' {print $1}'
25+ }
26+
2327# pre-flight checks
2428command -v inotifywait > /dev/null 2>&1 || error_exit " inotifywait not found, install inotify-tools"
2529command -v nginx > /dev/null 2>&1 || error_exit " nginx not found"
@@ -29,56 +33,38 @@ command -v nginx >/dev/null 2>&1 || error_exit "nginx not found"
2933LOG_DIR=$( dirname " ${RELOADER_LOG} " )
3034[[ -d " ${LOG_DIR} " ]] || mkdir -p " ${LOG_DIR} "
3135
32- log " Starting nginx-reloader"
33- log " Config: WATCH_DIR=${WATCH_DIR} DELAY=${RELOADER_DELAY} s LOG=${RELOADER_LOG} "
36+ # compute initial hash
37+ LAST_HASH=$( compute_hash)
38+
39+ log " Starting nginx-reloader v3"
40+ log " Config: WATCH_DIR=${WATCH_DIR} DELAY=${RELOADER_DELAY} s"
41+ log " Initial config hash: ${LAST_HASH} "
3442
3543while true
3644do
3745 sleep " ${RELOADER_DELAY} "
3846
39- # run inotifywait and capture both output and exit code
40- set +e
41- OUTPUT=$( inotifywait --recursive --include ' \.conf$' --format ' %w%f %e' \
42- -e create -e modify -e delete -e move " ${WATCH_DIR} " 2>&1 )
43- EXIT_CODE=$?
44- set -e
47+ # wait for any file event in watch dir
48+ inotifywait --recursive --format ' %w%f' \
49+ -e create -e modify -e delete -e move \
50+ " ${WATCH_DIR} " > /dev/null 2>&1 || true
4551
46- # handle inotifywait errors
47- if [[ ${EXIT_CODE} -ne 0 ]]; then
48- FAIL_COUNT=$(( FAIL_COUNT + 1 ))
49- log " inotifywait failed (exit=${EXIT_CODE} , count=${FAIL_COUNT} ): ${OUTPUT} "
50- if [[ ${FAIL_COUNT} -ge ${MAX_FAILS} ]]; then
51- error_exit " inotifywait failed ${MAX_FAILS} times, giving up"
52- fi
53- sleep 5
52+ # compute new hash of all .conf files
53+ NEW_HASH=$( compute_hash)
54+
55+ # skip if hash unchanged
56+ if [[ " ${NEW_HASH} " == " ${LAST_HASH} " ]]; then
5457 continue
5558 fi
5659
57- # reset fail count on success
58- FAIL_COUNT=0
59-
60- # parse output - format is "path event"
61- FILE=$( echo " ${OUTPUT} " | awk ' {print $1}' )
62- EVENT=$( echo " ${OUTPUT} " | awk ' {print $2}' )
63-
64- # skip if empty
65- [[ -z " ${FILE} " ]] && continue
66-
67- # only process .conf files (double check)
68- [[ " ${FILE} " != * .conf ]] && continue
69-
70- log " Detected: ${FILE} (${EVENT} )"
60+ log " Config changed: ${LAST_HASH} -> ${NEW_HASH} "
61+ LAST_HASH=" ${NEW_HASH} "
7162
7263 # test nginx config
73- set +e
74- TEST_OUTPUT=$( nginx -t 2>&1 )
75- TEST_CODE=$?
76- set -e
77-
78- if [[ ${TEST_CODE} -eq 0 ]]; then
64+ if nginx -t 2> /dev/null; then
7965 log " Config valid, reloading nginx"
80- nginx -s reload || log " Reload failed"
66+ nginx -s reload 2> /dev/null || log " Reload command failed"
8167 else
82- log " Config test failed: ${TEST_OUTPUT} "
68+ log " Config test failed, skipping reload "
8369 fi
8470done
0 commit comments