summaryrefslogtreecommitdiff
path: root/playbooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh
diff options
context:
space:
mode:
Diffstat (limited to 'playbooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh')
-rwxr-xr-xplaybooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh56
1 files changed, 56 insertions, 0 deletions
diff --git a/playbooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh b/playbooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh
new file mode 100755
index 0000000..967c2c0
--- /dev/null
+++ b/playbooks/roles/outbound/templates/proxy/nginx/dont-die-until-conn-closed.sh
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+echo "[/] sleeping to wait for some time for container to be marked as stop."
+# https://stackoverflow.com/a/45146086
+sleep 3
+
+pid_file="/run/nginx.pid"
+max_wait_seconds=30
+
+if [ ! -f "$pid_file" ]; then
+ echo "[!] Nginx PID file not found at $pid_file. Assuming Nginx not running or already stopped."
+ exit 0
+fi
+
+PID=$(cat "$pid_file")
+
+# Validate PID
+if [ -z "$PID" ] || ! [[ "$PID" =~ ^[0-9]+$ ]]; then
+ echo "[!] Invalid PID found in $pid_file: '$PID'"
+ exit 1
+fi
+
+# Check if the process actually exists before sending quit
+# kill -0 PID checks if a signal can be sent.
+if ! kill -0 "$PID" 2>/dev/null; then
+ echo "[!] Nginx process $PID not found or already stopped."
+ exit 0 # Exit successfully
+fi
+
+echo "[/] sending signal to nginx (PID: $PID) to quit"
+nginx -s quit
+
+start_time=$SECONDS
+echo "[/] Waiting for Nginx (PID: $PID) to stop (max ${max_wait_seconds}s)..."
+
+while [ -d /proc/$PID ]; do
+ current_time=$SECONDS
+ elapsed_time=$((current_time - start_time))
+
+ if [ "$elapsed_time" -ge "$max_wait_seconds" ]; then
+ echo "[!] Timeout: Nginx process $PID did not stop within ${max_wait_seconds} seconds."
+ echo "[!] Sending SIGKILL to PID $PID."
+ kill -9 "$PID" 2>/dev/null
+
+ exit 1
+ fi
+
+ sleep 0.5
+ if (( $(echo "$elapsed_time % 5" | bc) == 0 )); then
+ echo "[/] Nginx (PID: $PID) still running (waited ${elapsed_time}s)..."
+ fi
+done
+
+echo "[+] Nginx process $PID stopped gracefully."
+echo "[+] done. goodbye."
+exit 0