#!/bin/bash

# 检查是否提供了端口号
if [ -z "$1" ]; then
  echo "Usage: $0 <port>"
  exit 1
fi

# 查找指定端口的进程
PORT=$1
PIDS=$(lsof -t -i:$PORT)

# 检查是否找到了进程
if [ -z "$PIDS" ]; then
  echo "No process found listening on port $PORT."
  exit 0
fi

# 杀死这些进程
echo "Killing the following process(es) listening on port $PORT:"
echo $PIDS
kill $PIDS

# 检查进程是否被成功杀死
if [ $? -eq 0 ]; then
  echo "Process(es) on port $PORT terminated."
else
  echo "Failed to terminate process(es) on port $PORT."
fi