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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值