X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=scripts.git;a=blobdiff_plain;f=kill-unused-xterms.sh;h=075b2ba1acb948cfa2f688537476ae30ac60b4a3;hp=6ea3058a8a1153e34c78d8757cc7729ec2a4eab0;hb=c959baab4f9a2ba5b483a20336ef74b01704c6f7;hpb=2b983c5c7336826b764036e3e1478a5a8159248a diff --git a/kill-unused-xterms.sh b/kill-unused-xterms.sh index 6ea3058..075b2ba 100755 --- a/kill-unused-xterms.sh +++ b/kill-unused-xterms.sh @@ -23,14 +23,21 @@ set -e NB_KILLED=0 NB_TOTAL=0 -SHELL_NAME=bash +SHELL_NAME=$(basename ${SHELL}) +# Loop through the pid of xterms for ppid in $(ps h -C xterm -o pid); do + N=0 + + # For each, loop through the child shells for pid in $(ps h --ppid $ppid -o pid,cmd | awk '{ if($2 == "'${SHELL_NAME}'") { print $1 }}'); do - K=$(ps h --ppid $pid -o cmd,pid | wc -l) + # For each, count how many sub-process we do have + K=$(ps h --ppid $pid -o pid | wc -l) N=$((N+K+1)) done + + # If there is only one, kill the xterm if [[ $N == 1 ]]; then kill -USR2 $ppid NB_KILLED=$((NB_KILLED+1))