我想要杀死整个过程中树。什么是最好的方式,为此使用任何共同的脚本语言?我在寻找一个简单的解决方案。

有帮助吗?

解决方案

你不说,如果你想杀死树是一个进程组。 (这通常是这种情况,如果树是从服务器开始或外壳命令行分叉的结果。)可以使用GNU PS发现处理组如下:

 ps x -o  "%p %r %y %x %c "

如果这是你想杀死一个进程组,只需使用kill(1)命令,但不是给它一个进程号,给它的否定的组号码。例如,为了在组5112杀死每一道工序,使用kill -TERM -- -5112

其他提示

杀死属于同一进程的所有进程 进程树 使用 进程组ID (PGID)

  • kill -- -$PGID 使用默认信号(TERM = 15)
  • kill -9 -$PGID 使用信号 KILL (9)

您可以检索 PGID 从任何 进程ID (PID) 一样的 进程树

  • kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*') (信号 TERM)
  • kill -9 -$(ps -o pgid= $PID | grep -o '[0-9]*') (信号 KILL)

特别感谢 唐纳雀斯皮库斯 的贡献 $PID 剩余空间和 OSX 兼容性。

解释

  • kill -9 -"$PGID" => 发送信号 9 (KILL)给所有的孩子和孙子......
  • PGID=$(ps opgid= "$PID") => 检索 进程组ID 从任何 进程ID 树的,不仅是 进程父ID. 。的一个变体 ps opgid= $PIDps -o pgid --no-headers $PID 在哪里 pgid 可以替换为 pgrp.
    但:
    • ps 插入前导空格时 PID 少于五位数字并右对齐,如下所示 唐纳雀. 。您可以使用:
      PGID=$(ps opgid= "$PID" | tr -d ' ')
    • ps 从 OSX 总是打印标题,因此 斯皮库斯 建议:
      PGID="$( ps -o pgid "$PID" | grep [0-9] | tr -d ' ' )"
  • grep -o [0-9]* 仅打印连续数字(不打印空格或字母标题)。

更多命令行

PGID=$(ps -o pgid= $PID | grep -o [0-9]*)
kill -TERM -"$PGID"  # kill -15
kill -INT  -"$PGID"  # correspond to [CRTL+C] from keyboard
kill -QUIT -"$PGID"  # correspond to [CRTL+\] from keyboard
kill -CONT -"$PGID"  # restart a stopped process (above signals do not kill it)
sleep 2              # wait terminate process (more time if required)
kill -KILL -"$PGID"  # kill -9 if it does not intercept signals (or buggy)

局限性

  • 正如注意到的 大卫休伯特·卡里奥, , 什么时候 kill 由属于同一棵树的进程调用, kill 在终止整棵树的杀戮之前,它会冒着自杀的风险。
  • 因此,请确保使用具有不同的进程来运行该命令 进程组ID.

很长的故事

> cat run-many-processes.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
./child.sh background &
./child.sh foreground
echo "ProcessID=$$ ends ($0)"

> cat child.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
./grandchild.sh background &
./grandchild.sh foreground
echo "ProcessID=$$ ends ($0)"

> cat grandchild.sh
#!/bin/sh
echo "ProcessID=$$ begins ($0)"
sleep 9999
echo "ProcessID=$$ ends ($0)"

使用“&”在后台运行进程树

> ./run-many-processes.sh &    
ProcessID=28957 begins (./run-many-processes.sh)
ProcessID=28959 begins (./child.sh)
ProcessID=28958 begins (./child.sh)
ProcessID=28960 begins (./grandchild.sh)
ProcessID=28961 begins (./grandchild.sh)
ProcessID=28962 begins (./grandchild.sh)
ProcessID=28963 begins (./grandchild.sh)

> PID=$!                    # get the Parent Process ID
> PGID=$(ps opgid= "$PID")  # get the Process Group ID

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    28969 Ss   33021   0:00 -bash
28349 28957 28957 28349 pts/3    28969 S    33021   0:00  \_ /bin/sh ./run-many-processes.sh
28957 28958 28957 28349 pts/3    28969 S    33021   0:00  |   \_ /bin/sh ./child.sh background
28958 28961 28957 28349 pts/3    28969 S    33021   0:00  |   |   \_ /bin/sh ./grandchild.sh background
28961 28965 28957 28349 pts/3    28969 S    33021   0:00  |   |   |   \_ sleep 9999
28958 28963 28957 28349 pts/3    28969 S    33021   0:00  |   |   \_ /bin/sh ./grandchild.sh foreground
28963 28967 28957 28349 pts/3    28969 S    33021   0:00  |   |       \_ sleep 9999
28957 28959 28957 28349 pts/3    28969 S    33021   0:00  |   \_ /bin/sh ./child.sh foreground
28959 28960 28957 28349 pts/3    28969 S    33021   0:00  |       \_ /bin/sh ./grandchild.sh background
28960 28964 28957 28349 pts/3    28969 S    33021   0:00  |       |   \_ sleep 9999
28959 28962 28957 28349 pts/3    28969 S    33021   0:00  |       \_ /bin/sh ./grandchild.sh foreground
28962 28966 28957 28349 pts/3    28969 S    33021   0:00  |           \_ sleep 9999
28349 28969 28969 28349 pts/3    28969 R+   33021   0:00  \_ ps fj

命令 pkill -P $PID 不杀孙子:

> pkill -P "$PID"
./run-many-processes.sh: line 4: 28958 Terminated              ./child.sh background
./run-many-processes.sh: line 4: 28959 Terminated              ./child.sh foreground
ProcessID=28957 ends (./run-many-processes.sh)
[1]+  Done                    ./run-many-processes.sh

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    28987 Ss   33021   0:00 -bash
28349 28987 28987 28349 pts/3    28987 R+   33021   0:00  \_ ps fj
    1 28963 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh foreground
28963 28967 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28962 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh foreground
28962 28966 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28961 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh background
28961 28965 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999
    1 28960 28957 28349 pts/3    28987 S    33021   0:00 /bin/sh ./grandchild.sh background
28960 28964 28957 28349 pts/3    28987 S    33021   0:00  \_ sleep 9999

命令 kill -- -$PGID 杀死所有进程,包括孙进程。

> kill --    -"$PGID"  # default signal is TERM (kill -15)
> kill -CONT -"$PGID"  # awake stopped processes
> kill -KILL -"$PGID"  # kill -9 to be sure

> ps fj
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
28348 28349 28349 28349 pts/3    29039 Ss   33021   0:00 -bash
28349 29039 29039 28349 pts/3    29039 R+   33021   0:00  \_ ps fj

结论

我注意到在这个例子中 PIDPGID 是相等的(28957).
这就是为什么我最初认为 kill -- -$PID 就足够了。但在这种情况下,进程是在一个 Makefile进程号 不同于 组号.

我认为 kill -- -$(ps -o pgid= $PID | grep -o [0-9]*) 是从不同的调用时杀死整个进程树的最佳简单技巧 组号 (另一个进程树)。

pkill -TERM -P 27888

这会杀死具有父进程ID 27888的所有进程。

或者更健壮的:

CPIDS=$(pgrep -P 27888); (sleep 33 && kill -KILL $CPIDS &); kill -TERM $CPIDS

的时间表杀死33秒以后和礼貌的询问进程终止。

请参阅此答案用于终止所有后代。

要杀死的处理树递归,使用killtree():

#!/bin/bash

killtree() {
    local _pid=$1
    local _sig=${2:--TERM}
    kill -stop ${_pid} # needed to stop quickly forking parent from producing children between child killing and parent killing
    for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
        killtree ${_child} ${_sig}
    done
    kill -${_sig} ${_pid}
}

if [ $# -eq 0 -o $# -gt 2 ]; then
    echo "Usage: $(basename $0) <pid> [signal]"
    exit 1
fi

killtree $@

rkill 从命令 pslist 包,发送给出的信号(或 SIGTERM 通过默认)为指定的进程和它的所有后裔:

rkill [-SIG] pid/name...

布拉德的答案是什么,我建议过,但你可以用awk做掉干脆如果使用--ppid选项ps

for child in $(ps -o pid -ax --ppid $PPID) do ....... done

如果您知道通过父进程的PID,这里有一个shell脚本,它应该工作:

for child in $(ps -o pid,ppid -ax | \
   awk "{ if ( \$2 == $pid ) { print \$1 }}")
do
  echo "Killing child process $child because ppid = $pid"
  kill $child
done

在这里使用所描述的方法的一点点修改的版本: https://stackoverflow.com/a/5311362/563175

因此,看起来是:

kill `pstree -p 24901 | sed 's/(/\n(/g' | grep '(' | sed 's/(\(.*\)).*/\1/' | tr "\n" " "`

其中是24901父母的PID。

它看起来很丑陋,但它的工作完美。

志刚的回答的修改的版本:

#!/usr/bin/env bash
set -eu

killtree() {
    local pid
    for pid; do
        kill -stop $pid
        local cpid
        for cpid in $(pgrep -P $pid); do
            killtree $cpid
        done
        kill $pid
        kill -cont $pid
        wait $pid 2>/dev/null || true
   done
}

cpids() {
    local pid=$1 options=${2:-} space=${3:-}
    local cpid
    for cpid in $(pgrep -P $pid); do
        echo "$space$cpid"
        if [[ "${options/a/}" != "$options" ]]; then
            cpids $cpid "$options" "$space  "
        fi
    done
}

while true; do sleep 1; done &
cpid=$!
for i in $(seq 1 2); do
    cpids $$ a
    sleep 1
done
killtree $cpid
echo ---
cpids $$ a

我不能评论(没有足够的声望),所以我不得不添加一个新的答案的,即使这是不是一个真正的答案。

有与由@olibre年02月28给出的否则非常好的和完整的答案一个小问题ps opgid= $PID的输出将包含前导空格对于PID短于五个数字因为ps是有理由的柱(分辩对齐的数字)。内的整个命令行中,这导致负号,其次是空间(S),随后是组的PID。简单的解决方案是管pstr以除去位:

kill -- -$( ps opgid= $PID | tr -d ' ' )

要加入到诺曼·拉姆齐的答案,如果你想创建一个进程组,可能是值得考虑的,在setsid。结果 http://pubs.opengroup.org/onlinepubs/009695399/functions/setsid.html

  

在setsid()函数应设置一个   新的会话,如果调用进程   不是一个过程组长。上   返回调用进程应   这个新的会话组长   会议应进程组   一个新的进程组的领导者,   不得有控制终端。   调用的进程组ID   过程应被设定为等于   调用进程的进程ID。该   调用进程应是唯一的   新进程组在制造工艺和   在新的会话的唯一过程。

我认为此处的意思是,你可以从起动过程创建一个组。我为了用这个在PHP中可以启动它后杀死一个整体流程树。

这可能是一个好主意。我很想在注释中。

下面的壳功能类似于许多其他的答案,但它没有外部依赖性等pgrep工作既在Linux和BSD(OS X,等):

killtree() {
    local parent=$1 child
    for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do
        killtree $child
    done
    kill $parent
}

这是超级容易使用 psutil 蟒蛇做到这一点。只是PIP安装psutil,然后你有全套的工艺操作工具:

def killChildren(pid):
    parent = psutil.Process(pid)
    for child in parent.get_children(True):
        if child.is_running():
            child.terminate()

根据志刚的答案,这样就避免了自查杀:

init_killtree() {
    local pid=$1 child

    for child in $(pgrep -P $pid); do
        init_killtree $child
    done
    [ $pid -ne $$ ] && kill -kill $pid
}

如果你想的名字来杀死一个进程:

killall -9 -g someprocessname

pgrep someprocessname | xargs pkill -9 -g

这是我杀死使用bash脚本的所有子进程的版本。 它不使用递归,并且取决于p纤ep命令。

使用

killtree.sh PID SIGNAL

killtrees.sh的内容

#!/bin/bash
PID=$1
if [ -z $PID ];
then
    echo "No pid specified"
fi

PPLIST=$PID
CHILD_LIST=`pgrep -P $PPLIST -d,`

while [ ! -z "$CHILD_LIST" ]
do
    PPLIST="$PPLIST,$CHILD_LIST"
    CHILD_LIST=`pgrep -P $CHILD_LIST -d,`
done

SIGNAL=$2

if [ -z $SIGNAL ]
then
    SIGNAL="TERM"
fi
#do substring from comma to space
kill -$SIGNAL ${PPLIST//,/ }

下面是@志刚的回答这确实没有AWK,仅在击的本机解析可能性依赖的变化:

function killtree {
  kill -STOP "$1"
  ps -e -o pid= -o ppid= | while read -r pid ppid
                           do
                             [[ $ppid = $1 ]] || continue
                             killtree "$pid"  || true # Skip over failures
                           done
  kill -CONT "$1"          
  kill -TERM "$1"
}

这似乎在Mac和Linux的正常工作。在那里你可以不依赖于能够管理过程组的情况下 - 用于测试的软件必须建立在多种环境中编写脚本时像 - 这棵树行走技术是绝对有帮助的。

感谢您的智慧,乡亲们。我的剧本是离开退出一些子进程和否定的尖端使事情更加容易。我写这个函数在其他脚本中使用必要时:

# kill my group's subprocesses:          killGroup
# kill also myself:                      killGroup -x
# kill another group's subprocesses:     killGroup N  
# kill that group all:                   killGroup -x N
# N: PID of the main process (= process group ID).

function killGroup () {
    local prid mainpid
    case $1 in
        -x) [ -n "$2" ] && kill -9 -$2 || kill -9 -$$ ;;
        "") mainpid=$$ ;;
         *) mainpid=$1 ;;
    esac
    prid=$(ps ax -o pid,pgid | grep $mainpid)
    prid=${prid//$mainpid/}
    kill -9 $prid 2>/dev/null
    return
}

干杯。

有可能更好地杀死孩子之前的父母;否则他自杀前家长可以再次有可能产生新的孩子。这些将生存的杀伤。

我的PS的版本是从上述不同;也许是太旧,因此怪grepping ...

要使用一个外壳脚本,而不是一个壳函数具有许多优点...

但是,它基本上是zhigangs想法


#!/bin/bash
if test $# -lt 1 ; then
    echo >&2 "usage: kiltree pid (sig)"
fi ;

_pid=$1
_sig=${2:-TERM}
_children=$(ps j | grep "^[ ]*${_pid} " | cut -c 7-11) ;
echo >&2 kill -${_sig} ${_pid}
kill -${_sig} ${_pid}
for _child in ${_children}; do
    killtree ${_child} ${_sig}
done

下面的已经过测试在FreeBSD,Linux和MacOS X和仅取决于p纤ep和杀(-o版本不BSD下工作PS)。第一个参数是父PID,其中儿童有被终止。第二个参数是一个布尔值,以确定父PID是否有要太终止。

KillChilds() {
        local pid="${1}"
        local self="${2:-false}"

        if children="$(pgrep -P "$pid")"; then
                for child in $children; do
                        KillChilds "$child" true
                done
        fi

        if [ "$self" == true ]; then
                kill -s SIGTERM "$pid" || (sleep 10 && kill -9 "$pid" &)
        fi
}

KillChilds $$ > /dev/null 2>&1

这将发送SIGTERM任何儿童/孙子进程shell脚本中,如果SIGTERM没有成功,它会等待10秒钟,然后发送击杀。


此前答案:

在下文中也适用,但将杀死BSD壳本身。

KillSubTree() {
    local parent="${1}"
    for child in $(ps -o pid=$parent); do
            if [ $$ -ne $child ]; then (kill -s SIGTERM $child || (sleep 10 && kill -9 $child & )) > /dev/null 2>&1 ; fi
    done
}
# Example lanch from within script
KillSubTree $$ > /dev/null 2>&1

我开发志刚xyuri的溶液和solidsneck进一步:

 #!/bin/bash

if test $# -lt 1 ; then
    echo >&2 "usage: kiltree pid (sig)"
    exit 1 ;
  fi ;

_pid=$1
_sig=${2:-TERM}

# echo >&2 "killtree($_pid) mypid = $$"
# ps axwwf | grep -6 "^[ ]*$_pid " >&2 ;

function _killtree () {
    local _children
    local _child
    local _success

    if test $1 -eq $2 ; then # this is killtree - don't commit suicide!
        echo >&2 "killtree can´t kill it´s own branch - some processes will survive." ; 
        return 1 ;
      fi ;
    # this avoids that children are spawned or disappear.
    kill -SIGSTOP $2 ;

    _children=$(ps -o pid --no-headers --ppid $2) ;        
    _success=0 
    for _child in ${_children}; do
        _killtree $1 ${_child} $3 ;
        _success=$(($_success+$?)) ;
      done ;

    if test $_success -eq 0 ; then
        kill -$3 $2
      fi ;
    # when a stopped process is killed, it will linger in the system until it is continued
    kill -SIGCONT $2
    test $_success -eq 0 ;
    return $?
    }

_killtree $$ $_pid $_sig

这个版本将避免杀害它的血统 - 这会导致子进程的洪水在以前的解决方案

确定孩子列表之前

进程都正常停止,以便创建或消失没有新的孩子。

被杀害后,停止的作业必须继续从系统中消失。

老问题,我知道,但所有的反应似乎一直在打PS,我不喜欢。

此基于AWK-解决方案不需要递归和仅调用PS一次。

awk 'BEGIN {
  p=1390
  while ("ps -o ppid,pid"|getline) a[$1]=a[$1]" "$2
  o=1
  while (o==1) {
    o=0
    split(p, q, " ")
    for (i in q) if (a[q[i]]!="") {
      p=p""a[q[i]]
      o=1
      a[q[i]]=""
    }
  }
  system("kill -TERM "p)
}'

或者对单行:

awk 'BEGIN {p=1390;while ("ps -o ppid,pid"|getline) a[$1]=a[$1]" "$2;o=1;while (o==1) {o=0;split(p, q, " ");for (i in q) {if (a[q[i]]!="") {p=p""a[q[i]];o=1;a[q[i]]=""}}}system("kill -TERM "p)}'

基本上的想法是,我们建立了家长的数组(一):子条目,那么阵列发现我们的匹配父母的孩子周围循环,将其添加到我们的家长名单(P),因为我们去

如果你不想杀顶级流程,然后做

sub(/[0-9]*/, "", p)

之前的系统()行将从杀集合中移除。

时,要注意这里有一个竞争条件,但是这是真的(据我可以看到)所有的解决方案的。它做什么,我需要因为剧本我需要它不会产生很多短命的孩子。

为读者的运动将是使它成为一个2通环路:第一次通过之后,发送SIGSTOP所有进程在p列表中,则循环到PS再次运行和第二传输后发送SIGTERM,然后SIGCONT 。如果你不关心漂亮的结局,然后第二遍可能只是SIGKILL,我想。

如果您有pstree和Perl您的系统上,你可以试试这个:

perl -e 'kill 9, (`pstree -p PID` =~ m/\((\d+)\)/sg)'

如果你知道你想要杀死的事情PID,通常可以从会话ID,一切都在同一个会话中去。我倒是仔细检查,但我用这个脚本的在这我想死循环开始rsyncs,而不是再掀(因为循环的),因为它会如果我刚刚killall'd rsync的。

kill $(ps -o pid= -s $(ps -o sess --no-heading --pid 21709))

如果你不知道的PID你仍然可以嵌套多个

kill $(ps -o pid= -s $(ps -o sess --no-heading --pid $(pgrep rsync )))
ps -o pid= --ppid $PPID | xargs kill -9 

在SH的作业命令将列出的后台进程。在某些情况下,它可能是最好先杀掉最新的工艺,例如旧的一个创建的共享插座。在这种情况下,以相反的顺序排序的PID。有时候你要等待那一刻的工作写在磁盘或东西类似的东西,他们停止了。

如果你不就得了!不杀

for SIGNAL in TERM KILL; do
  for CHILD in $(jobs -s|sort -r); do
    kill -s $SIGNAL $CHILD
    sleep $MOMENT
  done
done

在外壳脚本杀死子进程:

很多时候我们需要杀死被吊死或块出于某种原因子进程。例如。 FTP连接问题。

有两种方法,

1)为每个子,其将监测和一旦超时达到杀死子过程创建单独的新的父。

创建test.sh如下,

#!/bin/bash

declare -a CMDs=("AAA" "BBB" "CCC" "DDD")
for CMD in ${CMDs[*]}; do
    (sleep 10 & PID=$!; echo "Started $CMD => $PID"; sleep 5; echo "Killing $CMD => $PID"; kill $PID; echo "$CMD Completed.") &
done
exit;

和观看哪个是使用下面的命令具有名称作为其他终端“测试”过程。

watch -n1 'ps x -o "%p %r %c" | grep "test" '

以上脚本将创建4个新的子进程和他们的父母。每个子进程将为10秒运行。但是,一旦达到5秒超时,thier各自的父进程将杀死这些孩子的。 所以,孩子将无法完成执行(10秒)。 围绕这些定时播放(开关10和图5),以查看另一个行为。在这种情况下孩子它到达10秒的超时之前将完成在5秒执行。

2)设当前父监视器,并且一旦达到超时杀子进程。这不会创建单独的父母来监控每一个孩子。您也可以正确地管理同父之内的所有子进程。

创建test.sh如下,

#!/bin/bash

declare -A CPIDs;
declare -a CMDs=("AAA" "BBB" "CCC" "DDD")

CMD_TIME=15;
for CMD in ${CMDs[*]}; do
    (echo "Started..$CMD"; sleep $CMD_TIME; echo "$CMD Done";) &
    CPIDs[$!]="$RN";
    sleep 1;
done

GPID=$(ps -o pgid= $$);
CNT_TIME_OUT=10;
CNT=0;
while (true); do
    declare -A TMP_CPIDs;

    for PID in "${!CPIDs[@]}"; do
        echo "Checking "${CPIDs[$PID]}"=>"$PID;

        if ps -p $PID > /dev/null ; then
          echo "-->"${CPIDs[$PID]}"=>"$PID" is running..";
          TMP_CPIDs[$PID]=${CPIDs[$PID]};
        else
          echo "-->"${CPIDs[$PID]}"=>"$PID" is completed.";
        fi
    done

    if [ ${#TMP_CPIDs[@]} == 0 ]; then
        echo "All commands completed.";
        break;
    else
        unset CPIDs;
        declare -A CPIDs;
        for PID in "${!TMP_CPIDs[@]}"; do
            CPIDs[$PID]=${TMP_CPIDs[$PID]};
        done
        unset TMP_CPIDs;

        if [ $CNT -gt $CNT_TIME_OUT ]; then
            echo ${CPIDs[@]}"PIDs not reponding. Timeout reached $CNT sec. killing all childern with GPID $GPID..";
            kill -- -$GPID;
        fi
    fi

    CNT=$((CNT+1));
    echo "waiting since $b secs..";
    sleep 1;
done

exit;

和观看哪个是使用下面的命令具有名称作为其他终端“测试”过程。

watch -n1 'ps x -o "%p %r %c" | grep "test" '

以上脚本将创建4个新的子进程。我们存储所有的子进程的PID和循环他们,要检查自己是否执行完毕或仍在运行。 子进程将执行至CMD_TIME时间。但是,如果CNT_TIME_OUT超时到达,所有的孩子都将获得由父进程杀死。 您可以切换时间和玩的脚本看到的行为。 这种方法的一个缺点是,它使用组ID杀死所有子树。但是,父进程本身属于同一组所以它也将被杀死。

您可能需要分配其他组ID父进程,如果你不希望父母被杀害做。

的更多细节可以在这里找到,

杀子进程在外壳脚本

此脚本也工作:

#/bin/sh while true do echo "Enter parent process id [type quit for exit]" read ppid if [ $ppid -eq "quit" -o $ppid -eq "QUIT" ];then exit 0 fi for i in `ps -ef| awk '$3 == '$ppid' { print $2 }'` do echo killing $i kill $i done done

我知道是旧的,但是这是更好的解决方案,我发现:

killtree() { 
    for p in $(pstree -p $1 | grep -o "([[:digit:]]*)" |grep -o "[[:digit:]]*" | tac);do
        echo Terminating: $p 
        kill $p
    done
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top