aboutsummaryrefslogtreecommitdiff
path: root/scripts/test_scripts/kill-test-processes.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/test_scripts/kill-test-processes.sh')
-rwxr-xr-xscripts/test_scripts/kill-test-processes.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/scripts/test_scripts/kill-test-processes.sh b/scripts/test_scripts/kill-test-processes.sh
new file mode 100755
index 000000000..e5fb2fcaa
--- /dev/null
+++ b/scripts/test_scripts/kill-test-processes.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+#
+# Kill leftover CI test processes (zenserver, minio, nomad, consul) whose
+# executable lives under the given build directory.
+#
+# Used by CI workflows to clean up any test processes from a previous run
+# before starting a new one, and to reap any that are still running after
+# the run finishes.
+#
+# Usage: kill-test-processes.sh <label> <build_dir>
+# label: word printed in log messages, e.g. "stale" or "leftover"
+# build_dir: absolute path; only processes whose executable is under this
+# directory are killed.
+#
+# We resolve each PID's actual executable path rather than relying on argv
+# (which `pgrep -a` reports). argv starts with the short process name (e.g.
+# "consul") regardless of how the process was launched, so a pure argv prefix
+# match never fires and leftovers silently survived between runs.
+
+set -u
+
+label=${1:?label required}
+build_dir=${2:?build_dir required}
+
+get_exe_path() {
+ local pid=$1
+ if [[ -r "/proc/$pid/exe" ]]; then
+ # Linux
+ readlink -f "/proc/$pid/exe" 2>/dev/null || true
+ else
+ # macOS fallback: the "txt" file descriptor points at the process binary
+ lsof -p "$pid" -Fn -a -d txt 2>/dev/null | awk '/^n/{print substr($0,2); exit}' || true
+ fi
+}
+
+for name in zenserver minio nomad consul; do
+ while read -r pid; do
+ [[ -z "$pid" ]] && continue
+ exe=$(get_exe_path "$pid")
+ if [[ "$exe" == "$build_dir"* ]]; then
+ echo "Killing $label $name (PID $pid): $exe"
+ kill -9 "$pid" 2>/dev/null || true
+ fi
+ done < <(pgrep -x "$name" 2>/dev/null || true)
+done