NGINX配置文件变更监控 重新载入nginx reload生效 基于MD5的
这个是基于MD5的
不依赖其他
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#!/bin/bash # NGINX WATCH DAEMON # # # Place file in root of nginx folder: /etc/nginx # This will test your nginx config on any change and # if there are no problems it will reload your configuration # USAGE: sh nginx-watch.sh # Set NGINX directory # tar command already has the leading / dir='etc/nginx' # Get initial checksum values checksum_initial=$(tar --strip-components=2 -C / -cf - $dir | md5sum | awk '{print $1}') checksum_now=$checksum_initial # Start nginx nginx # Daemon that checks the md5 sum of the directory # ff the sums are different ( a file changed / added / deleted) # the nginx configuration is tested and reloaded on success while true do checksum_now=$(tar --strip-components=2 -C / -cf - $dir | md5sum | awk '{print $1}') if [ $checksum_initial != $checksum_now ]; then echo '[ NGINX ] A configuration file changed. Reloading...' nginx -t && nginx -s reload; fi checksum_initial=$checksum_now sleep 2 done |
wget https://raw.githubusercontent.com/devotox/nginx-watch/master/nginx-watch.sh -O /usr/bin/nginx-watch
chmod +x /usr/bin/nginx-watch
sudo nginx-watch
/usr/bin/nginx-watch