-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_with_env.sh
57 lines (49 loc) · 1.68 KB
/
run_with_env.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
51
52
53
54
55
56
57
#!/bin/ash
# Manage the domains
echo "Starting manage_domains.sh script..."
./manage_domains.sh
echo "Finished manage_domains.sh script."
# Stop the currently running Nginx service
echo "Checking if Nginx is running..."
if [ -f /var/run/nginx.pid ]; then
echo "Forcefully stopping Nginx..."
nginx -s quit || kill -SIGTERM $(cat /var/run/nginx.pid) 2>/dev/null || kill -SIGKILL $(cat /var/run/nginx.pid) 2>/dev/null
echo "Wait 5 seconds..."
sleep 5
else
echo "Nginx is not running. Skipping stop command."
fi
# Function to replace environment variables in template files
process_template() {
local template="$1"
local output_file="${template%.template}"
# Use 'envsubst' to replace environment variables
envsubst "$(printf '${%s} ' $(env | cut -d '=' -f 1))" < "$template" > "$output_file"
# (Optional) You can read and do something with the config file here
local config
config=$(cat "$output_file")
}
iterate_directory() {
local dir="$1"
for item in "$dir"/* "$dir"/.*; do
if [[ $(basename "$item") == "." || $(basename "$item") == ".." ]]; then
# skip current and parent directory references
continue
fi
if [ -d "$item" ]; then
# Inside the process_template function
echo "Processing folder: $item"
iterate_directory "$item"
elif [[ "$(basename "$item")" == *.template ]]; then
# Inside the process_template function
echo "Processing template: $item"
process_template "$item"
fi
done
}
# Starting directory
start_dir="/etc/nginx/conf.d"
iterate_directory "$start_dir"
# Start Nginx
echo "Start Nginx again..."
nginx -g "daemon off;"