Killing process via shell script

#!/bin/sh

#Check input parameter
if [ $# -ne 2 ]
then
        echo "Usage: `basename $0` {KEY_WORD} {PORT-NUMBER}"
        exit -1
else 
        echo "Key word of the process: $1"
        RET=`expr match $2 "[0-9]*$"`
        if [ "$RET" -gt 0 ]; then
                echo "Port number is: $2"
        else
                echo "Invalid port number: $2"
                exit -1
        fi
fi

check_count()
{
        arr=(${pids})
        length=${#arr[*]}
        if [  ${length} -gt 1 ]; then
                pids=`echo ${pids} | tr "[\n ]" ","`
                pids=${pids%,*}
                echo "Error: Found more than one process: ${pids}"
                exit -1
        fi
}

pids=$(ps -ef | grep gfitps | grep "$1" | grep $2 | grep -v grep | grep -v $$ | grep -v `basename $0` | awk '{print $2}')

if [ "${pids}" != "" ]; then
        check_count
        echo "Killing process ${pids}"
        kill -9 ${pids}
        echo "Killed process ${pids} successfully"
else
        echo "No instance running"
fi

猜你喜欢

转载自huanyue.iteye.com/blog/2017430