美文网首页
杀死进程的shell脚本

杀死进程的shell脚本

作者: 枫海 | 来源:发表于2017-02-09 09:53 被阅读0次
#!/bin/sh

NAME=$1
ExactSearches=$2
KilAll=$3
ForceKillProcess=$4
ProcessLive=$5


#判断参数是否为空,空则提示其使用方法
if [ -z "$NAME" ];then
    echo "Usage:"
    echo "示例:./killprocess.sh QQ N Y Y N"
    echo "第一个参数为:进程名称,注意严格区分大小写"
    echo "第二个参数为:精确搜索(Y)/模糊搜索(N),默认精确搜索"
    echo "第三个参数为:结束所有匹配到的进程(Y)/结束匹配到的第一个进程(N),默认全部结束"
    echo "第四个参数为:强制结束进程(Y)/非强制结束进程(N),默认强制结束进程"
    echo "第五个参数为:显示匹配到进程存活情况(Y)/不显示匹配到进程存活情况(N),默认显示存活情况"
    echo "注意:脚本结束会生成processResult.txt显示详情"
exit
fi

case "$1" in
-h|--help)
    echo "Usage:"
    echo "示例:./killprocess.sh QQ N Y Y N"
    echo "第一个参数为:进程名称,注意严格区分大小写"
    echo "第二个参数为:精确搜索(Y)/模糊搜索(N),默认精确搜索"
    echo "第三个参数为:结束所有匹配到的进程(Y)/结束匹配到的第一个进程(N),默认全部结束"
    echo "第四个参数为:强制结束进程(Y)/非强制结束进程(N),默认强制结束进程"
    echo "第五个参数为:显示匹配到进程存活情况(Y)/不显示匹配到进程存活情况(N),默认显示存活情况"
    echo "注意:脚本结束会生成processResult.txt显示详情"
exit
;;
date)
    echo `date +%Y-%m-%d---%H:%M:%S`
exit
esac


if [ -e "tempProcessList.txt" ]; then
    rm tempProcessList.txt
fi

if [ -e "processResult.txt" ]; then
    rm processResult.txt
fi

#传入pid杀死进程
killProcessFun(){
if test "N" = "$ForceKillProcess"
then
    kill $1 2>/dev/null
else
    kill -9 $1 2>/dev/null
fi
}


echo "进程名称:$NAME"
echo "进程名称:$NAME" >> processResult.txt

if test "N" = "$KilAll"
then
    echo "结束进程方式:非强制结束" >> processResult.txt
    echo "结束进程方式:非强制结束"
else
    echo "结束进程方式:强制结束" >> processResult.txt
    echo "结束进程方式:强制结束"
fi

#通过输入参数筛选出ID
if test "N" = "$ExactSearches"
then
    echo "搜索方式:模糊搜索"
    echo "搜索方式:模糊搜索" >> processResult.txt
    #ID=`ps -ef | grep "$NAME" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
    ID=`pgrep "$NAME"`
else
    echo "搜索方式:精确搜索"
    echo "搜索方式:精确搜索" >> processResult.txt
    #ID=`ps -ef | grep "\<$NAME\>" | grep -v "$0" | grep -v "grep" | awk '{print $2}'`
    ID=`pgrep -x "$NAME"`
fi

echo "相关进程pid:"
echo "相关进程pid:" >> processResult.txt
echo $ID
echo $ID >> processResult.txt

echo ">>>--------------搜索结果详情------------------\n" >> processResult.txt
echo ">>>--------------搜索结果详情------------------\n"

#输出相关进程信息
for id in $ID
do
    detail=`ps -ef | grep "$id" | grep -v "grep"| awk '{print $0}'`
    echo "pid:$id" >> processResult.txt
    echo "pid:$id"
    echo "detail:$detail\n" >> processResult.txt
    echo "detail:$detail\n"
    echo "$id|||$detail" >> tempProcessList.txt
done
    echo "-----------------搜索结果详情---------------<<<" >> processResult.txt
    echo "-----------------搜索结果详情---------------<<<"


echo "\\n\\n\\n>>>--------------处理目标进程------------------\n" >> processResult.txt
echo "\\n\\n\\n>>>--------------处理目标进程------------------\n"

#遍历ID结束相关进程
for id in $ID
do
    detail=`ps -ef | grep "$id" | grep -v "grep"| awk '{print $0}'`

    #如果结束全部
    if test "N" = "$KilAll"
    then

        echo "killing_pid:$id"
        echo "killing_pid:$id" >> processResult.txt
        echo "detail:$detail"
        echo "detail:$detail" >> processResult.txt
        killProcessFun $id

    break
    else
        echo "killing_pid:$id" >> processResult.txt
        echo "killing_pid:$id"

        while read LINE
        do
            str=$LINE

            if test "$id" = "${str%%|||*}"
            then
                echo "detail:${LINE#*|||}\n" >> processResult.txt
                echo "detail:${LINE#*|||}\n"
            fi

        done < tempProcessList.txt
        killProcessFun $id
    fi
done

echo "-----------------处理目标进程---------------<<<" >> processResult.txt
echo "-----------------处理目标进程---------------<<<"



if test "N" = "$processLive"
then
echo " "
else
echo "\\n\\n\\n>>>--------------目标进程处理结果------------------\n" >> processResult.txt
echo "\\n\\n\\n>>>--------------目标进程处理结果------------------\n"

for id in $ID
do
detail=`ps -ef | grep "$id" | grep -v "grep"| awk '{print $0}'`
isLive='kill -0 $id'

    if kill -0 $id 2>/dev/null; then
        echo "pid(存活):$id" >> processResult.txt
        echo "pid(存活):$id"
        echo "detail:$detail\n" >> processResult.txt
        echo "detail:$detail\n"

    else
        echo "pid(死亡):$id" >> processResult.txt
        echo "pid(死亡):$id"
        while read LINE
        do
            str=$LINE
            if test "$id" = "${str%%|||*}"
            then
                echo "detail:${LINE#*|||}\n" >> processResult.txt
                echo "detail:${LINE#*|||}\n"
            break
            fi
        done < tempProcessList.txt
    fi

done

echo "-----------------目标进程处理结果---------------<<<" >> processResult.txt
echo "-----------------目标进程处理结果---------------<<<"

fi

if [ -e "tempProcessList.txt" ]; then
    rm tempProcessList.txt
fi

相关文章

网友评论

      本文标题:杀死进程的shell脚本

      本文链接:https://www.haomeiwen.com/subject/ukkiittx.html