[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Approaches for killing left-behind processes /Re: killer in the manual



El dom, 08-11-2009 a las 14:13 +0100, RalfGesellensetter escribió:
> Am Friday, 6. November 2009 schrieb RalfGesellensetter:
> > Dear José, 
> > 
> > thank you for sharing your interesting script
> > (and supporting my approach this way). 
> > It is charmingly short - thanks to the power of Gambas 
> > (even more charming!).
> 
> As your script is quite short - and most likely under GPL, 
> I dare attach it to this mail - so everybody can see the
> nice syntax of Gambas Basic.
> 
> I might be able to test the script next Friday.
> > 
> > I think I remember that it is not a big deal anymore to meet 
> > the dependencies for running Gambas script - so I'd suggest 
> > to add the package needed (gambas2-runtime?), as it won't be trivial 
> > to tranlate your script to a shell [perl] script!?
> 
> I remember that the Gambas IDE has an export filter to Debian packages.
> Assuming that all dependencies are set as needed, it shouldn't be a big
> deal to provide a first package for this.
> 
> Sometimes, prolongued peeking pays off ;)
> 
> Regards & thanks again
> Ralf


You've been faster than me ;)

As many people don't feel confident with gambas I have translated the
script into python. It's attached to this email. For this script to
work, it must be executed as root.

Consider it as a draft to a possible and more elegant solution. I think
that some config options should be added to it. 
Now if you want to run it in background, you have to execute:
killer.py &
And if you want to change the time between "killings", the line at the
end with
time.sleep(5)
should change the 5 seconds to any other name.

Anyway, I've tested it a little bit and seems to work exactly as the
gambas version we've been running for a long time.

On the other hand, in Debian lenny there is a gambas package called:
gambas2-script, installing it you can run any gambas code as the
previous one, just adding:
#!/usr/bin/env gbs2
as the first line of the text file (and giving exec permissions to the
file)

Hope this helps.
Regards

#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
# Project:     Killer
# Module:     killer.py
# Purpose:     First draft of a simple killer.
# Language:    Python 2.5
# Date:        8-Nov-2009.
# Ver:        28-Nov-2009.
# Author:    José L. Redrejo Rodríguez
# Copyright:   2009 - José L. Redrejo Rodríguez    <jredrejo @nospam@ debian.org>
#
# killer.py is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# HMIServer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with killer.py. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################


import sys,os
import signal
import time
import commands


class Killer():        
    
    def _getLoggedUsers(self):
        logged=commands.getoutput("who|cut -f1 -d' '|uniq" )
        loggedusers=logged.splitlines()
        uids={}
        if len(loggedusers) != 0:
            for user in loggedusers:
                newID=commands.getoutput("getent passwd " + user + "|cut -f3 -d:")
                uids[newID]=user
                 
        return uids
                    
    def searchZombies(self):
        print "searching targets"
        uids=self._getLoggedUsers()
        processes=commands.getoutput("ps -e -o euid= -o pid=|tr -s ' '")    
        pids=processes.splitlines()
        for line in pids:
            bits=line.split()
            if len(bits)>2:
                continue
            uid=int(bits[0])
            if uid>=1000 and uid<>65534 and not uids.has_key(bits[0]):
                self._killAllChildren(bits[1])
            
        
        
    def _killAllChildren(self,ppid):
        result=commands.getoutput("ps -A -o %p,%P|grep " + ppid  + "|cut -f1 -d,")
        for child in result.splitlines():
            os.system ("kill -9 " + child + " 2>/dev/null")
  

def handler(signum, frame):
    print 'No more murders today'
    sys.exit()

signal.signal(signal.SIGINT, handler)

myKiller=Killer()


while True:
    time.sleep(5)
    myKiller.searchZombies()


Attachment: signature.asc
Description: Esta parte del mensaje =?ISO-8859-1?Q?est=E1?= firmada digitalmente


Reply to: