执行步骤
chmod +x cp.sh
sudo ./cp.sh 8080
#文件存储为cp.sh
#!/bin/bash
# 检查是否提供了端口号
if [ $# -ne 1 ]; then
echo "Usage: $0 <port>"
exit 1
fi
PORT=$1
# 查找使用该端口的进程 ID
PID=$(lsof -t -i :$PORT)
# 检查是否找到进程
if [ -z "$PID" ]; then
echo "No process found on port $PORT."
exit 1
fi
# 杀死进程
echo "Killing process $PID on port $PORT..."
kill -9 $PID
if [ $? -eq 0 ]; then
echo "Process killed successfully."
else
echo "Failed to kill process."
fi