有时候莫名其妙程序启动不了就是应为端口被战用了,使用这个脚本可以轻松解决问题
lsofPORT
使用方法
./lsofPORT 端口 密码
普通用户下 ./lsofPORT 12345 abc123
ROOT用户下 ./lsofPORT 12345
#!/bin/bash
PORT="$1"
MYqassword="$2"
#---------------------------
#./lsofPORT 端口 密码
#普通用户下 ./lsofPORT 12345 abc123
#ROOT用户下 ./lsofPORT 12345
#---------------------------
NAME_ID=`getent passwd ${SUDO_UID:-$(id -u)} | cut -d: -f 6`
MY_USER_DIR="${NAME_ID}"
NAME='root'
if [[ ! X${NAME} = X${MY_USER_DIR} ]];then
# 普通用户
mkdir -p /tmp/lsofPORT
shijian=`date +%Y年%m月%d日%H:%M:%S`
echo "${MYqassword}" | sudo -S $shijian > /dev/null 2>&1
sudo lsof -i :${PORT}|grep "${PORT}"|awk '{print $1}' > ${MY_USER_DIR}/占用端口的程序.txt
sudo lsof -i :${PORT}|grep "${PORT}"|awk '{print $2}' > /tmp/lsofPORT/lsofPORT.log
kill_X=`cat /tmp/lsofPORT/lsofPORT.log`
sudo kill -9 ${kill_X}
sudo rm -rf /tmp/lsofPORT
else
# root
lsof -i :${PORT}|grep "${PORT}"|awk '{print $1}' > ${MY_USER_DIR}/占用端口的程序.txt
kill_X=`lsof -i :${PORT}|grep "${PORT}"|awk '{print $2}'`
kill -9 ${kill_X}
fi
exit 0
评论区