Linux根据进程名字彻底删除所有相关的子进程

您所在的位置:网站首页 kill命令的使用 Linux根据进程名字彻底删除所有相关的子进程

Linux根据进程名字彻底删除所有相关的子进程

#Linux根据进程名字彻底删除所有相关的子进程| 来源: 网络整理| 查看: 265

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3