Open terminal killer

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

Open terminal killer

#1 Post by jpeps »

...for some reason, I've been ending up with a zillion open terminals lately..so..

Code: Select all

#!/bin/sh

i="0"
VAR=($(ps | grep "rxvt" | grep -v "grep" | awk '{print $1}'))

while (( ${VAR[$i]} )) ; do

kill -9  "${VAR[$i]}"
 i=$((i+1))

done

User avatar
puppyluvr
Posts: 3470
Joined: Sun 06 Jan 2008, 23:14
Location: Chickasha Oklahoma
Contact:

#2 Post by puppyluvr »

:D Hello,
Why are you ending up with open terminals???
Zombies, orphaned child processes???
Close the Windows, and open your eyes, to a whole new world
I am Lead Dog of the
Puppy Linux Users Group on Facebook
Join us!

Puppy since 2.15CE...

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#3 Post by jpeps »

puppyluvr wrote::D Hello,
Why are you ending up with open terminals???
Zombies, orphaned child processes???

"You cannot kill zombies, as they are already dead"

nah.....I just do almost everything from a hot-keyed terminal. They might be open to different directories, or I'm doing some process repeatedly (so I can hit the arrow for that terminal)...or I just lost track of it (like it's on a different desktop) while opening a new one to do something completely different.

User avatar
runtt21
Posts: 1649
Joined: Sun 08 Jun 2008, 02:43
Location: BigD Texas
Contact:

#4 Post by runtt21 »

"You cannot kill zombies"


Yes you can, from the rc.shutdown script:

Code: Select all

killzombies() {
 ZOMBIES="`ps -H -A | grep '<defunct>' | sed -e 's/  /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
 for ONEZOMBIE in $ZOMBIES
 do
  echo "Killing parentless zombie process $ONEZOMBIE"
  kill $ONEZOMBIE
 done
}

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#5 Post by jpeps »

runtt21 wrote:"You cannot kill zombies"


Yes you can, from the rc.shutdown script:
By chance, are you related to George Bush?

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#6 Post by technosaurus »

i usually just do
killall rxvt (or urxvt)

have you considered using mrxvt - you can open 100 tabbed terminals in the same window - much lower on resources that way too ... just sayin'
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#7 Post by jpeps »

technosaurus wrote:i usually just do
killall rxvt (or urxvt)
:lol: guess that's a little easier!
have you considered using mrxvt - you can open 100 tabbed terminals in the same window - much lower on resources that way too ... just sayin'
Never heard of it....great idea! Works nicely. Thanks

edit: Wow...did a pet install and it took on the rxvt settings....
Attachments
mrxvt.png
(13.51 KiB) Downloaded 646 times

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#8 Post by big_bass »

I call this kill_rxvt and I made it a script on my desktop
I have been using this for a long time
it can be used as a kill by name just by replacing rxvt with some other command


Code: Select all

pid=$(ps aux | grep "rxvt" | awk '{print $2}')&& kill -9 $pid
Joe

big_bass
Posts: 1740
Joined: Mon 13 Aug 2007, 12:21

#9 Post by big_bass »

double post

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#10 Post by jpeps »

big_bass wrote:I call this kill_rxvt and I made it a script on my desktop
I have been using this for a long time
it can be used as a kill by name just by replacing rxvt with some other command


Code: Select all

pid=$(ps aux | grep "rxvt" | awk '{print $2}')&& kill -9 $pid
Joe
I was having fun playing with arrays :), but there's simpler ways like killall

Code: Select all

pid=$(pidof rxvt) && kill -9 $pid
interesting note: "$pid" won't work

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#11 Post by technosaurus »

grep is a pita with ps, but did you know all of the pids are actually directories in /proc? If you did then you may already know that you can use readlink to find out the name each pid was invoked by

Code: Select all

get_pids(){
for x in /proc/*/exe ;do
 case `readlink $x` in
  *${1}*)x=${x#/proc/};x=${x%/exe};echo $x;;
 esac
done
}

kill `get_pids epdfview`
or make your own killall (some smaller distros don't have it, and kill is a shell builtin, so this should be even faster than killall)

Code: Select all

killall(){
for x in /proc/*/exe ;do
 case `readlink $x` in
  *${1}*)x=${x#/proc/};x=${x%/exe};kill $x;;
 esac
done
}
if you wanted to take it a bit further you could use some string manipulations so that you could use $@ instead of $1, but that's not what I was trying to show here
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#12 Post by jpeps »

technosaurus wrote:grep is a pita with ps, but did you know all of the pids are actually directories in /proc? If you did then you may already know that you can use readlink to find out the name each pid was invoked by

Code: Select all

get_pids(){
for x in /proc/*/exe ;do
 case `readlink $x` in
  *${1}*)x=${x#/proc/};x=${x%/exe};echo $x;;
 esac
done
}

kill `get_pids epdfview`
I counted 119 instances of the link being "/bin/bash" *
Tabbed instances of the mrxvt terminal get only one process.

*edit: a few minutes later, the number had increased to 134

Code: Select all

count(){
i="0"
for x in /proc/*/exe ;do
 var=$(readlink  $x)
 var2=$(echo $var | grep  ${1})
  [ $var2 ] &&  i=$((i+1)) 
done 
echo "$i"
}

count "/bin/bash"
119

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#13 Post by technosaurus »

jpeps wrote: I counted 119 instances of the link being "/bin/bash" *
Tabbed instances of the mrxvt terminal get only one process.

*edit: a few minutes later, the number had increased to 134
I've had a lot, but not _that_ many ... oh wait yes I did, with mrxvt - seems like if you kill mrxvt it doesn't kill the shells that it spawns
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#14 Post by jpeps »

technosaurus wrote:
jpeps wrote: I counted 119 instances of the link being "/bin/bash" *
Tabbed instances of the mrxvt terminal get only one process.

*edit: a few minutes later, the number had increased to 134
I've had a lot, but not _that_ many ... oh wait yes I did, with mrxvt - seems like if you kill mrxvt it doesn't kill the shells that it spawns
Loading terminals or scripts quickly adds /bin/bash processes that remain. I've put an icon on my tray that kills them. Running X needs a few of them however, so it's necessary to record /bin/bash processes present right after startup.

In /root/startup:

Code: Select all

#!/bin/sh

## initilize ps-bash. Create do-not-delete list on Startup

  for x in /proc/*/exe ;do
     var=$(readlink  $x | grep "/bin/bash")
        if [ $var ]; then
           x=${x#/proc/} ;x=${x%/exe}
        echo ${x} >>/tmp/ba
        fi
   done
Next, it's necessary to ignore processes that occur when running the present script, so I run a wrapper first that sets a time mark before running the actual kill script. It's also a good place to set a warning to close all terminals first. For just checking processes, run from terminal with [ -c ]. This goes in /usr/local/bin

Code: Select all

#!/bin/sh


## to count processes from terminal, USAGE: [ -c]

var=$(pidof mrxvt)
 if [ "$var" -a "$1 != "-c" ]; then 
    Xdialog --msgbox "Close terminals" 5 25
    exit
 fi 

touch /tmp/mark
/usr/local/ps-bash/ps-bash.sh $@

The actual ps-bash.sh script. The idea is to kill /bin/bash processes with the exception of the current process and those present right after booting. The script also shows the present count.

Code: Select all

#!/bin/sh


##  Save initial bash processes to /tmp/ba  
##  Kill subsequent bash processes 
  if [ "$1" == "-h" ]; then   
     echo 'Usage ps-bash [-i -k]
         initialize
          kill additional processes
          '
      exit
  fi

export op="$@"


init(){
  [ -f /tmp/ba ] && rm /tmp/ba

  for x in /proc/*/exe ;do
     var=$(readlink  $x | grep "/bin/bash")
        if [ $var ]; then 
           x=${x#/proc/} ;x=${x%/exe}
           echo ${x} >>/tmp/ba  
        fi  
   done 
}


count(){

##  current process
    [ -f /tmp/cur ] && rm /tmp/cur

i="0"
for x in /proc/*/exe ;do
 var=$(readlink  $x | grep "/bin/bash")
 
 if [ "$var" ]; then 
     i=$((i+1))
   cur=$(find ${x} -newer /tmp/mark) 
     
    
      x=${x#/proc/} ;x=${x%/exe}
   [ "$cur" ] && echo ${x} >>/tmp/cur 
    var2=$(cat /tmp/ba | grep  ${x})
   #  echo "line 46: ${var2}"
      [ -f /tmp/cur ] && var3=$(cat /tmp/cur | grep ${x})
   #    echo "line 48 ${var3}"
    if [ ! "$var2" -a ! "$var3" ]; then 
   #    echo "line 50 ${x}"
      if [ "$op" == "-k" -a ! "$var2" -a ! "$var3" ]; then
         echo "killing ${x}" 
         kill -9  ${x}
      fi
    fi
  fi
done 
#echo "${i} /bin/bash processes"
}

if [ "$1" == "-i" ]; then  
   init
else
   count
   count
Xdialog --title "ps-bash" --msgbox  "${i} /bin/bash processes"  5 30 
fi

Attachments
ps-bash.png
(7.38 KiB) Downloaded 554 times

Shep
Posts: 878
Joined: Sat 08 Nov 2008, 07:55
Location: Australia

#15 Post by Shep »

jpeps wrote:

Code: Select all

pid=$(pidof rxvt) && kill -9 $pid
interesting note: "$pid" won't work
It will work if there is only one rxvt process. Otherwise, you're instructing it to interpret your list of PIDs as a single long PID containing embedded spaces (i.e., quite invalid).

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#16 Post by jpeps »

Shep wrote:
jpeps wrote:

Code: Select all

pid=$(pidof rxvt) && kill -9 $pid
interesting note: "$pid" won't work
It will work if there is only one rxvt process. Otherwise, you're instructing it to interpret your list of PIDs as a single long PID containing embedded spaces (i.e., quite invalid).
I guess it's more interesting that it works without quotes, since it's not really an array. ${x[0]} is the whole list either way. Yet you can perform separate actions on elements if unquoted.

Code: Select all

pid=$(pidof rxvt) 

for x in "$pid"; do
echo "ID ${x}"
done

for x in $pid; do
echo "ID ${x}"
done

Post Reply