Linux有些时候kill -9进程pid,进程名字还会出现,比如spark提交应用时的SparkSubmit。这是因为当前进程有其它子进程依赖。此时可以根据进程名字彻底删除,这里我提供了一份模板:
使用命令很超级简单,把你的进程名字扔进模板里:
bash recursionKillProcess.sh yourProcessName
recursionKillProcess.sh的详细内容如下所示:
#!/bin/bash
# This script is used to kill the process which is running in the background.
#查看参数个数
if [ $# -eq 1 ];
then
#获取当前用户名
processName=$1
#获取当前用户名以及用户名长度,用于分割获取进程号
username=`whoami`
userNameLen=${#username}
#进程号一般为5位
startIndex=$(($userNameLen+1))
endIndex=$(($userNameLen+7))
#kill SparkSubmit
mainPid=`ps -ef |grep $processName |grep -v 'grep' |head -1|cut -c ${startIndex}-${endIndex}`
#获取当前主进程及其所有子线程pid
pidLine=`pstree -p ${mainPid}`
pidLine=`echo $pidLine | awk 'BEGIN{ FS="(" ; RS=")" } NF>1 { print $NF }'`
for pid in $pidLine
do
kill -9 ${pid}
done
echo " ${processName} has stopped!"
else
#杀死SparkSubmit进程及其所有子线程
echo "You need define the name of process which you want to kill."
echo "e.g., bash recursionKillProcess.sh SparkSubmit"
fi
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)