-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
80 lines (64 loc) · 1.75 KB
/
run.sh
File metadata and controls
80 lines (64 loc) · 1.75 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
set -euo pipefail
# Function to log messages
log() {
local message="$1"
local timestamp
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "$timestamp $message"
}
# Function to cleanly stop processes
cleanup() {
log "Caught signal or error, stopping Nginx..."
if [[ -n "${nginx_pid-}" ]]; then
kill -SIGTERM "$nginx_pid" 2>/dev/null || true
wait "$nginx_pid" 2>/dev/null || true
fi
log "All processes have been stopped. Exiting."
exit 1
}
# Function to check the status of a process
check_process() {
local pid=$1
local name=$2
if ! kill -0 "$pid" 2>/dev/null; then
log "$name has terminated unexpectedly."
cleanup
fi
}
# Trap termination signals and errors
trap cleanup SIGINT SIGTERM ERR
AUTH_CONF_FILE="/etc/nginx/conf.d/auth.conf"
HTPASSWD_FILE="/etc/nginx/.htpasswd"
echo "" >$AUTH_CONF_FILE
# Setup authentication if environment variables are set
if [[ -n "${USERNAME-}" && -n "${PASSWORD-}" ]]; then
log "Setting up HTTP basic authentication..."
htpasswd -bc "$HTPASSWD_FILE" "$USERNAME" "$PASSWORD"
echo 'auth_basic "Restricted Content";' >$AUTH_CONF_FILE
echo 'auth_basic_user_file '"$HTPASSWD_FILE"';' >>$AUTH_CONF_FILE
else
log "No HTTP basic authentication will be used."
fi
log "Starting Nginx..."
nginx -g 'daemon off;' &
nginx_pid=$!
if ! kill -0 $nginx_pid 2>/dev/null; then
log "Failed to start Nginx."
cleanup
fi
# Background loop to monitor the processes
while true; do
check_process $nginx_pid "Nginx"
sleep 1
done &
# Wait for all processes to finish
wait $nginx_pid
nginx_status=$?
# Check the exit statuses
if [[ $nginx_status -ne 0 ]]; then
cleanup
else
log "All processes completed successfully. Exiting."
exit 0
fi