我想殺死整個程序樹。 使用任何常用腳本語言執行此操作的最佳方法是什麼? 我正在尋找一個簡單的解決方案。
#1樓
志剛答案的修改版:
#!/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
#2樓
受ysth的評論啟發
kill -- -PGID
與其給它一個程序号,不給它組号。 與幾乎所有指令一樣,如果您希望以開頭的普通參數不被解釋為開關,請在其前面加上
-
--
#3樓
使用程序組ID (
PGID
)殺死屬于同一程序樹的所有程序
-
使用預設信号(kill -- -$PGID
= 15)TERM
-
使用信号kill -9 -$PGID
(9)KILL
您可以從同一程序樹的任何程序ID (
PID
)中檢索
PGID
。
-
(信号kill -- -$(ps -o pgid= $PID | grep -o '[0-9]*')
)TERM
-
(信号kill -9 -$(ps -o pgid= $PID | grep -o '[0-9]*')
)KILL
特别感謝唐加拉雀和Speakus的捐款
$PID
剩餘空間和OSX的相容性。
說明
-
=>向所有子孫發送信号9(kill -9 -"$PGID"
)...KILL
-
=>從樹的任何Process-ID中檢索Process-Group-ID ,而不僅僅是Process-Parent-ID 。PGID=$(ps opgid= "$PID")
變體是ps opgid= $PID
,其中ps -o pgid --no-headers $PID
可以用pgid
pgrp
代替。
但:
-
在ps
小于五位數且如tanager注意到的那樣右對齊時插入前導空格。 您可以使用:PID
PGID=$(ps opgid= "$PID" | tr -d ' ')
- 來自OSX的
總是列印标題,是以Speakus建議:ps
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)
局限性
- 由于注意到了達維德和休伯特Kario ,當
由屬于同一棵樹的過程調用,kill
風險終止整個樹殺害前殺死自己。kill
- 是以,請確定使用具有不同Process-Group-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
結論
在此示例中,我注意到
PID
和
PGID
相等(
28957
)。
這就是為什麼我最初認為
kill -- -$PID
就足夠了。 但是,如果程序在
Makefile
則程序ID與組ID不同。
我認為
kill -- -$(ps -o pgid= $PID | grep -o [0-9]*)
是從另一個組ID (另一個程序樹)調用時殺死整個程序樹的最佳簡單技巧。
#4樓
以下shell函數與許多其他答案類似,但是它可以在Linux和BSD(OS X等)上運作,而無需像
pgrep
這樣的外部依賴項:
killtree() {
local parent=$1 child
for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do
killtree $child
done
kill $parent
}
#5樓
使用psutil使用python做到這一點非常容易。 隻需将psutil與pip一起安裝,即可獲得一整套的流程處理工具:
def killChildren(pid):
parent = psutil.Process(pid)
for child in parent.get_children(True):
if child.is_running():
child.terminate()
#6樓
我不能評論(沒有足夠的聲望),是以我不得不增加一個新的答案 ,盡管這不是一個真正的答案。
@olibre在2月28日給出的否則,可能會得到一個非常好的和徹底的回答,這有一個小問題
ps opgid= $PID
的輸出将包含少于五位數的PID的前導空格,因為
ps
使列對齊(數字)。 在整個指令行中,這将導緻一個負号,後跟空格,然後是組PID。 一個簡單的解決方案是将
ps
傳遞給
tr
以删除空格:
kill -- -$( ps opgid= $PID | tr -d ' ' )
#7樓
根據志剛的回答,這可以避免自我殺戮:
init_killtree() {
local pid=$1 child
for child in $(pgrep -P $pid); do
init_killtree $child
done
[ $pid -ne $$ ] && kill -kill $pid
}
#8樓
這是@zhigang答案的一種變體,它沒有AWK,僅依賴于Bash的本機解析能力:
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上似乎都可以正常工作。 在您不能依賴于能夠管理流程組的情況下(例如編寫腳本來測試必須在多個環境中建構的軟體時),這種周遊樹技術無疑會有所幫助。
#9樓
如果要按名稱終止程序:
killall -9 -g someprocessname
要麼
pgrep someprocessname | xargs pkill -9 -g
#10樓
在sh中,jobs指令将列出背景程序。 在某些情況下,最好先終止最新的程序,例如較舊的程序建立了一個共享套接字。 在這些情況下,請按相反順序對PID進行排序。 有時,您需要等待片刻,以便作業在停止之前将其寫入磁盤或類似内容。
而且,如果不必,也不要殺死!
for SIGNAL in TERM KILL; do
for CHILD in $(jobs -s|sort -r); do
kill -s $SIGNAL $CHILD
sleep $MOMENT
done
done
#11樓
要遞歸終止程序樹,請使用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 $@
#12樓
在shell腳本中殺死子程序:
很多時候我們需要殺死由于某種原因而挂起或阻塞的子程序。 例如。 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;
使用以下指令在其他終端中監視名稱為“ test”的程序。
watch -n1 'ps x -o "%p %r %c" | grep "test" '
上面的腳本将建立4個新的子程序及其父母。 每個子程序将運作10秒。 但是一旦達到5秒逾時,它們各自的父程序将殺死那些孩子。 是以孩子将無法完成執行(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;
使用以下指令在其他終端中監視名稱為“ test”的程序。
watch -n1 'ps x -o "%p %r %c" | grep "test" '
上面的腳本将建立4個新的子程序。 我們将存儲所有子程序的pid,并對其進行循環以檢查它們是否已完成執行或仍在運作。 子程序将執行到CMD_TIME時間。 但是,如果CNT_TIME_OUT逾時到達,則所有子程序都将被父程序殺死。 您可以切換時間并使用腳本來檢視行為。 這種方法的一個缺點是,它使用組ID殺死所有子樹。 但是父程序本身屬于同一組,是以也會被殺死。
如果您不想殺死父程序,則可能需要将其他組ID配置設定給父程序。
更多詳情可在這找到,
在Shell腳本中殺死子程序
#13樓
以下内容已在FreeBSD,Linux和MacOS X上進行了測試,僅取決于pgrep和kill(ps -o版本在BSD下不起作用)。 第一個參數是必須終止其子級的父級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秒鐘,然後發送kill。
較早的答案:
以下内容也可以,但是會殺死BSD上的shell本身。
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
#14樓
該腳本也可以工作:
#/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
#15樓
如果您的系統上有pstree和perl,則可以嘗試以下操作:
perl -e 'kill 9, (`pstree -p PID` =~ m/\((\d+)\)/sg)'
#16樓
pslist軟體包中的rkill指令将給定信号(預設情況下為
SIGTERM
)發送到指定的程序及其所有後代:
rkill [-SIG] pid/name...
#17樓
如果您知道傳遞父程序的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
#18樓
布拉德的答案也是我的建議,除了如果對
ps
使用
--ppid
選項,可以完全取消
awk
。
for child in $(ps -o pid -ax --ppid $PPID) do ....... done
#19樓
您沒有說您要殺死的樹是否是單個程序組。 (如果樹是從伺服器啟動或shell指令行派生的結果,通常是這種情況。)可以使用GNU ps如下發現程序組:
ps x -o "%p %r %y %x %c "
如果它是您要終止的程序組,則隻需使用
kill(1)
指令,但不要給它一個程序号,而要給它取反組号。 例如,要殺死組5112中的每個程序,請使用
kill -TERM -- -5112
。
#20樓
我将進一步開發zhigang,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
此版本将避免殺死其祖先-在先前的解決方案中導緻大量子程序。
在确定子級清單之前,程序已正确停止,是以不會建立或消失任何新的子級。
在被殺死之後,停止的作業必須繼續從系統中消失。
#21樓
謝謝你的智慧,夥計們。 我的腳本是在退出時保留一些子程序, 否定提示使事情變得更容易。 如有必要,我編寫了此函數以供其他腳本使用:
# 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
}
幹杯。
#22樓
我知道這是一個老問題,但是所有的回答似乎都一直在呼喚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)}'
基本上,我們的想法是建立一個由parent:child條目組成的數組(a),然後在數組中循環查找比對的父級的子級,然後将它們添加到我們的父級清單(p)中。
如果您不想終止頂層程序,那麼可以這樣做
sub(/[0-9]*/, "", p)
就在system()行将其從kill set中删除之前。
請記住,這裡有一個競争條件,但就所有解決方案而言,這都是事實(據我所知)。 它滿足了我的需要,因為我需要的腳本不會建立很多短暫的孩子。
讀者的一個練習是使其成為2遍循環:在第一遍之後,将SIGSTOP發送到p清單中的所有程序,然後循環以再次運作ps,在第二遍之後發送SIGTERM,然後是SIGCONT。 我想,如果您不在乎出色的結局,那麼第二遍可能隻是SIGKILL。
#23樓
我知道那是舊的,但這是我發現的更好的解決方案:
killtree() {
for p in $(pstree -p $1 | grep -o "([[:digit:]]*)" |grep -o "[[:digit:]]*" | tac);do
echo Terminating: $p
kill $p
done
}
#24樓
最好先殺掉父母,再殺掉孩子。 否則,父母可能會在殺死自己之前再次生下新孩子。 這些将幸免于難。
我的ps版本與上述版本不同; 也許是太老了,是以奇怪的是...
使用shell腳本而不是shell函數有很多優點...
但是,這基本上是志剛的想法
#!/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
#25樓
如果您知道要殺死的對象的pid,通常可以使用會話ID以及同一會話中的所有内容。 我會仔細檢查,但是我将其用于在我想死的循環中啟動rsync的腳本,而不是像我隻是殺死所有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 )))
#26樓
ps -o pid= --ppid $PPID | xargs kill -9
#27樓
pkill -TERM -P 27888
這将殺死所有具有父程序ID 27888的程序。
或更強大:
CPIDS=$(pgrep -P 27888); (sleep 33 && kill -KILL $CPIDS &); kill -TERM $CPIDS
計劃在33秒後殺死,并禮貌地要求程序終止。
請參閱此答案以終止所有後代。
#28樓
要增加Norman Ramsey的答案,如果您想建立一個流程組,可能值得看一下setid。
http://pubs.opengroup.org/onlinepubs/009695399/functions/setsid.html
如果調用程序不是程序組負責人,則setsid()函數應建立一個新會話。 傳回時,呼叫程序将是此新會話的會話負責人,應是新程序組的程序組負責人,并且沒有控制終端。 調用程序的程序組ID必須設定為等于調用程序的程序ID。 調用程序應是新程序組中的唯一程序,并且是新會話中的唯一程序。
我的意思是,您可以從開始過程中建立一個組。 我在php中使用了它,以便能夠在啟動它之後殺死整個程序樹。
這可能是一個壞主意。 我會對評論感興趣。
#29樓
這是我使用bash腳本殺死所有子程序的版本。 它不使用遞歸,并且取決于pgrep指令。
使用
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//,/ }
#30樓
我使用此處描述的方法的一些修改版本: https : //stackoverflow.com/a/5311362/563175
是以看起來像這樣:
kill `pstree -p 24901 | sed 's/(/\n(/g' | grep '(' | sed 's/(\(.*\)).*/\1/' | tr "\n" " "`
其中24901是父母的PID。
它看起來很醜,但是做得很好。