Skip to content

Stop_simulator.sh

#!/bin/bash
# stop_simulator.sh — robust stop with colored output + final summary
set -o pipefail

# ===== Colors =====
GREEN="\033[1;32m"
RED="\033[1;31m"
YELLOW="\033[1;33m"
NC="\033[0m"

ok_count=0
warn_count=0
err_count=0

# Always print a summary, even if something fails
summary() {
  echo
  if (( err_count > 0 )); then
    echo -e "${RED}[ERROR] $err_count error(s), ${YELLOW}$warn_count warn(s), ${GREEN}$ok_count ok${NC}"
    echo -e "${RED}[INFO] Simulator stop completed with errors.${NC}"
  else
    echo -e "${GREEN}[OK] $ok_count action(s) ok, ${YELLOW}$warn_count warn(s)${NC}"
    echo -e "${GREEN}[INFO] Simulator stop completed successfully.${NC}"
  fi
}
trap summary EXIT

echo -e "${YELLOW}[INFO] Stopping simulator processes and freeing ports...${NC}"

# ===== Config =====
PORTS=(1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512)
PROCS=(
  "ffrMeter_simulator_v5.py"
  "fss_simulator_v1.py"
  "pcs_simulator_v3.py"
  "sensorB_simulator_v2.py"
  "solarMeter_simulator_v6.py"
  "ac_simulator_v1.py"
  "bcms_simulator_v14.py"
  "gridMeter_simulator_v7.py"
)

have_sudo_cached() { sudo -n true 2>/dev/null; }
maybe_sudo() {
  # Use sudo only if already cached to avoid stalling for a password
  if have_sudo_cached; then sudo "$@"; else "$@"; fi
}

kill_pids_hard() {
  local pids=("$@")
  [[ ${#pids[@]} -eq 0 ]] && return 0
  kill -KILL "${pids[@]}" 2>/dev/null
}

kill_pids_soft_then_hard() {
  local pids=("$@")
  [[ ${#pids[@]} -eq 0 ]] && return 0
  kill -TERM "${pids[@]}" 2>/dev/null
  sleep 0.5
  if ps -p "${pids[@]}" >/dev/null 2>&1; then
    kill_pids_hard "${pids[@]}"
  fi
}

kill_port() {
  local port="$1"
  local pids=()

  # Prefer listening sockets; fall back to any socket on port
  mapfile -t pids < <(maybe_sudo lsof -t -i :"$port" -sTCP:LISTEN 2>/dev/null | sort -u)
  if [[ ${#pids[@]} -eq 0 ]]; then
    mapfile -t pids < <(maybe_sudo lsof -t -i :"$port" 2>/dev/null | sort -u)
  fi

  if [[ ${#pids[@]} -eq 0 ]]; then
    echo -e "${YELLOW}[WARN] No process found on port $port${NC}"
    ((warn_count++))
    return
  fi

  if kill_pids_soft_then_hard "${pids[@]}"; then
    # Re-check if anything still holds the port
    if maybe_sudo lsof -i :"$port" 2>/dev/null | grep -q .; then
      echo -e "${RED}[ERROR] Port $port is still in use after kill attempts.${NC}"
      ((err_count++))
    else
      echo -e "${GREEN}[OK] Freed port $port (killed: ${pids[*]})${NC}"
      ((ok_count++))
    fi
  else
    echo -e "${RED}[ERROR] Failed to kill PID(s) ${pids[*]} on port $port${NC}"
    ((err_count++))
  fi
}

kill_proc() {
  local name="$1"
  local pids=()
  mapfile -t pids < <(pgrep -f "$name" 2>/dev/null | sort -u)

  if [[ ${#pids[@]} -eq 0 ]]; then
    echo -e "${YELLOW}[WARN] No running process found for: $name${NC}"
    ((warn_count++))
    return
  fi

  if kill_pids_soft_then_hard "${pids[@]}"; then
    if pgrep -f "$name" >/dev/null 2>&1; then
      echo -e "${RED}[ERROR] Some '$name' processes are still running after kill attempts.${NC}"
      ((err_count++))
    else
      echo -e "${GREEN}[OK] Killed process(es) for: $name (PID: ${pids[*]})${NC}"
      ((ok_count++))
    fi
  else
    echo -e "${RED}[ERROR] Failed to kill process: $name (PID: ${pids[*]})${NC}"
    ((err_count++))
  fi
}

# ===== Execute =====
for port in "${PORTS[@]}"; do
  kill_port "$port"
done

for proc in "${PROCS[@]}"; do
  kill_proc "$proc"
done