在系统维护中,经常会遇到端口被未知进程占用的情况。现在写了个脚本来帮助我们干掉那些占用我们端口的进程。目前这个版本还只适合于Solaris系统。
#!/bin/bash
SCRIPT_NAME==`echo $0 | awk '{print substr($0, 3)}' | awk -v FS="." '{print $1}'`
if [ $# -ne 1 ]
then
echo "usage ${SCRIPT_NAME} portNum"
exit 1
fi
portNum=$1
cd /proc
for pid in *
do
process=`pfile ${pid} | grep -i "port: ${portNum}"`
if [ "`echo ${process} | grep ${portNum}`" ]
then
echo "port: ${portNum} is using by process ${pid}(pid)."
ps -ef | grep ${pid}
while [ 1 ]
do
printf "Kill it or not? Y/N?: (Y)"; read ans
if [ "${ans}" == "Y" || "${ans}" == "y" ]
then
kill -9 ${pid}
break
fi
done
fi
done
本文分享了一个用于Solaris系统的Shell脚本,该脚本可以帮助用户查找并终止占用指定端口的进程。通过输入端口号作为参数,脚本会搜索所有运行中的进程,并列出占用目标端口的进程ID及其相关信息,随后询问用户是否终止这些进程。

被折叠的 条评论
为什么被折叠?



