
次のスクリプトがありますtest.sh
#!/bin/bash
function cleanup() {
echo "calling cleanup"
echo "PIDs associated with $0"
ps aux | grep $0
echo "killing $$ $(jobs -p)"
kill $(jobs -p | xargs)
exit 0
}
function subshell() (
echo "subshell pid $BASHPID"
while true
do
echo "subshell running..."
sleep 5
done
)
function no_subshell() {
echo "no_subshell pid $BASHPID"
while true
do
echo "no_subshell running..."
sleep 10
done
}
# Main
echo "test.sh pid $$"
trap "exit" INT TERM SIGUSR1
trap cleanup EXIT
subshell &
no_subshell &
echo "Waiting..."
jobs -l
wait
私の期待は、親プロセスでSIGINTが呼び出されると機能もクリーンtest.sh
アップする必要があるということです。しかし、実際に私が観察したことは次のとおりです。subshell
no_subshell
./test.sh
test.sh pid 49199
Waiting...
[1]- 49200 Running subshell &
[2]+ 49201 Running no_subshell &
no_subshell pid 49201
no_subshell running...
subshell pid 49202
subshell running...
^Ccalling cleanup
PIDs associated with ./test.sh
root 49199 0.0 0.0 106144 2896 pts/1 S+ 03:43 0:00 /bin/bash ./test.sh
root 49200 0.0 0.0 106144 232 pts/1 S+ 03:43 0:00 /bin/bash ./test.sh
root 49201 0.0 0.0 106144 2248 pts/1 S+ 03:43 0:00 /bin/bash ./test.sh
root 49202 0.0 0.0 106144 2248 pts/1 S+ 03:43 0:00 /bin/bash ./test.sh
root 49206 0.0 0.0 103488 2184 pts/1 S+ 03:43 0:00 grep ./test.sh
killing 49199 49200
49201
SIGINTの後、subshell
関数はPID 49202で実行され続けます。この値はjobs -l
私のcleanup
関数にリストされていません。
px aux | grep test
root 49202 0.0 0.0 106144 2248 pts/1 S 03:43 0:00 /bin/bash ./test.sh
さらに、サブシェル機能はPID 49200によって追跡されて報告されているように見えますがjobs -p
(おそらく終了した可能性があります)、サブシェル機能はPID 49202にまだ存在しているようです。
私が観察したこの行動をどのように説明できますか?test.sh
バックグラウンドで実行されているサブシェル機能をどのように整理できますか?
答え1
killコマンドは、同じプロセスグループ(PPID)に属するプロセスのみを終了します。プログラムPPIDは2896のようですが、subshellとno_subshell PPIDは2248です。