#!/usr/bin/env bash set -euo pipefail PORT="${MODEL_SELECTOR_PORT:-3300}" get_pids_with_lsof() { if command -v lsof >/dev/null 2>&1; then lsof -ti "tcp:${PORT}" 2>/dev/null || true fi } get_pids_with_fuser() { if command -v fuser >/dev/null 2>&1; then fuser "${PORT}/tcp" 2>/dev/null | tr ' ' '\n' | awk 'NF { print }' || true fi } pids="$( get_pids_with_lsof get_pids_with_fuser )" if [[ -z "${pids}" ]]; then exit 0 fi printf '%s\n' "${pids}" | sort -u | while read -r pid; do [[ -z "${pid}" ]] && continue kill "${pid}" 2>/dev/null || true done sleep 1 remaining="$( get_pids_with_lsof get_pids_with_fuser )" if [[ -n "${remaining}" ]]; then printf '%s\n' "${remaining}" | sort -u | while read -r pid; do [[ -z "${pid}" ]] && continue kill -9 "${pid}" 2>/dev/null || true done fi