杀死指定端口的进程
#!/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
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员小航
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果