kill占用指定端口的进程

在系统维护中,经常会遇到端口被未知进程占用的情况。现在写了个脚本来帮助我们干掉那些占用我们端口的进程。目前这个版本还只适合于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,脚本,bash)