运行了几个python后台守护进程,需要重启的时候不能简单 killall python 很容易会误杀其他服务
所以备忘以下的脚本

已验证有效

#!/bin/sh

function PidFind()  
{
    PIDCOUNT=`ps -ef | grep $1 | grep -v "grep" | grep -v $0 | awk '{print $2}' | wc -l`;  

    if [ ${PIDCOUNT} -gt 1 ] ; then  
        echo "There are too many process contains name[$1]"  
    elif [ ${PIDCOUNT} -le 0 ] ; then  
        echo "No such process[$1]!"  
    else  
        PID=`ps -ef | grep $1 | grep -v "grep" | grep -v ".sh" | awk '{print $2}'` ;  
        echo "Find the PID of this progress!--- process:$1 PID=[${PID}] ";  

        echo "Kill the process $1 ...";          
        kill -9  ${PID};  
        echo "kill -9 ${PID} $1 done!";  
    fi  
}
#查找并kill掉 带有 newdoc.daemon 名称的进程
PidFind newdoc.daemon

#restart daemon
/path/to/startscript/chkdaemon.sh

当搜索结果不唯一时,脚本也会拒绝执行杀进程

参考文:https://jingyan.baidu.com/article/b7001fe1a5dbc40e7382dd75.html

标签: linux, python, shell, 进程

添加新评论