Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 21 Oct 2018, 21:38
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Open terminal killer
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
Author Message
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sun 20 Nov 2011, 20:26    Post subject:  Open terminal killer  

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

Code:

#!/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
Back to top
View user's profile Send private message 
puppyluvr


Joined: 06 Jan 2008
Posts: 3482
Location: Chickasha Oklahoma

PostPosted: Sun 20 Nov 2011, 20:48    Post subject:  

Very Happy 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...
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sun 20 Nov 2011, 22:20    Post subject:  

puppyluvr wrote:
Very Happy 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.
Back to top
View user's profile Send private message 
runtt21


Joined: 07 Jun 2008
Posts: 1651
Location: BigD Texas

PostPosted: Sun 20 Nov 2011, 22:30    Post subject:  

"You cannot kill zombies"


Yes you can, from the rc.shutdown script:

Code:
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
}
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sun 20 Nov 2011, 22:34    Post subject:  

runtt21 wrote:
"You cannot kill zombies"


Yes you can, from the rc.shutdown script:


By chance, are you related to George Bush?
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4841
Location: Blue Springs, MO

PostPosted: Mon 21 Nov 2011, 05:11    Post subject:  

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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Mon 21 Nov 2011, 06:07    Post subject:  

technosaurus wrote:
i usually just do
killall rxvt (or urxvt)

Laughing guess that's a little easier!

Quote:

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....
mrxvt.png
 Description   
 Filesize   13.51 KB
 Viewed   632 Time(s)

mrxvt.png

Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1742

PostPosted: Mon 21 Nov 2011, 16:51    Post subject:  

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:

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


Joe
Back to top
View user's profile Send private message 
big_bass

Joined: 13 Aug 2007
Posts: 1742

PostPosted: Mon 21 Nov 2011, 17:00    Post subject:  

double post
Back to top
View user's profile Send private message 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Mon 21 Nov 2011, 17:31    Post subject:  

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:

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


Joe


I was having fun playing with arrays Smile, but there's simpler ways like killall
Code:

pid=$(pidof rxvt) && kill -9 $pid


interesting note: "$pid" won't work
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4841
Location: Blue Springs, MO

PostPosted: Mon 21 Nov 2011, 19:49    Post subject:  

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:
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:
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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Tue 22 Nov 2011, 03:10    Post subject:  

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:
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:

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
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4841
Location: Blue Springs, MO

PostPosted: Tue 22 Nov 2011, 03:46    Post subject:  

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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
jpeps

Joined: 31 May 2008
Posts: 3217

PostPosted: Sun 27 Nov 2011, 22:57    Post subject:  

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:

#!/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:

#!/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:

#!/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

ps-bash.png
 Description   
 Filesize   7.38 KB
 Viewed   545 Time(s)

ps-bash.png

Back to top
View user's profile Send private message 
Shep

Joined: 08 Nov 2008
Posts: 880
Location: Australia

PostPosted: Wed 30 Nov 2011, 01:52    Post subject:  

jpeps wrote:
Code:

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).
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 2 [16 Posts]   Goto page: 1, 2 Next
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0769s ][ Queries: 12 (0.0101s) ][ GZIP on ]