-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnginx.run
33 lines (25 loc) · 803 Bytes
/
nginx.run
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
#!/bin/sh
set -u
mkdir -p /var/run/
NGINX_PID_FILE="/run/nginx.pid"
NGINX_CONFIG="/etc/nginx/nginx.conf"
NGINX_SITES="/etc/nginx/sites-enabled"
NGINX_CMD="nginx -c $NGINX_CONFIG"
NGINX_CHECK_CONFIG_CMD="$NGINX_CMD -t"
log() {
if [[ "$@" ]]; then echo "[`date +'%Y-%m-%d %T'`] $@";
else echo; fi
}
log "waiting 1 seconds"
sleep 1
$NGINX_CHECK_CONFIG_CMD
$NGINX_CMD
log "Nginx started with $NGINX_CONFIG config, pid $(cat $NGINX_PID_FILE)."
# Check if config has changed
# Note: also monitor /etc/hosts where the new back-end hosts might be provided.
while inotifywait -q -e create,delete,modify,attrib $NGINX_CONFIG $NGINX_SITES; do
log "Restarting NGINX due to config changes..."
$NGINX_CHECK_CONFIG_CMD
$NGINX_CMD -s reload
log "NGINX restarted, pid $(cat $NGINX_PID_FILE)."
done