-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathnginx-reload.sh
executable file
·50 lines (40 loc) · 1.15 KB
/
nginx-reload.sh
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
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
log() {
date_time="$(date +%Y-%m-%d\ %H:%M:%S)"
if [ -z $2 ]; then
echo "${date_time} nginx: $1"
else
(>&2 echo "${date_time} nginx: ERROR: $1")
fi
}
{
log "starting"
nginx -g 'daemon off;' "$@" && exit 1
} &
nginx_pid=$!
watches=${NGINX_WATCH_PATHS:-"/etc/nginx/nginx.conf"}
config_file=${NGINX_CONFIG_FILE:-"/etc/nginx/nginx.conf"}
log "setting up watches for ${watches[@]}"
{
log "pid $nginx_pid"
inotifywait -r -q -e modify,move,create,delete --format '%w %e %T' -m --timefmt '%H%M%S' \
${watches[@]} | while read file event tm; do
current=$(date +'%H%M%S')
delta=`expr $current - $tm`
log "at ${tm} config file ${file} update detected (${event})"
if [ $delta -lt 2 -a $delta -gt -2 ] ; then
sleep 2 # sleep 1 set to let file operations end
log "will test config $config_file"
nginx -t -c $config_file
if [ $? -ne 0 ]; then
log "new configuration is invalid!!" 1
else
log "new configuration is valid, reloading"
nginx -s reload
fi
fi
done
log "inotifywait failed, killing nginx" 1
kill -TERM $nginx_pid
} &
wait $nginx_pid || exit 1