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

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



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
STATIC PUBLIC SUB KillAllChildren(PPID AS Integer)
DIM result AS String
DIM bits AS String[]
DIM child AS String
  SHELL "ps -A -o %p,%P|grep " & CStr(PPID) & "|cut -f1 -d," TO result
  bits = Split(result, "\n")
  FOR EACH child IN bits
    TRY SHELL "kill -9 " & child & " 2>/dev/null"
  NEXT  
END

STATIC PUBLIC SUB Killer()
DIM pids AS String[]
DIM uids AS Collection
DIM bits AS String[]
DIM tmpShell AS String
DIM sTmp AS String
DIM uid AS Integer

'get logged users:
  SHELL "who|cut -f1 -d\" \"|uniq" TO tmpShell
  bits = Split(Trim$(tmpShell), "\n")
  IF bits.Count = 0 THEN RETURN 
  uids = NEW Collection(gb.Text)
  FOR EACH sTmp IN bits
    SHELL "getent passwd " & sTmp & "|cut -f3 -d:" TO tmpShell 'this works even with ldap users
    tmpShell = Trim$(tmpShell)
    TRY uids.Add(sTmp, tmpShell) 
  NEXT 
  
'get current processes
  SHELL "ps -e -o euid= -o pid=|tr -s \" \"" TO tmpShell
  pids = Split(Trim$(tmpShell), "\n")
  FOR EACH sTmp IN pids
    bits = Split(Trim$(sTmp), " ")
    IF bits.Count < 2 THEN CONTINUE 
    uid = CInt(bits[0])
    'uid's under 1000 are ignored and nobody user too, as it's used by some needed daemons:
    IF uid >= 1000 AND uid <> 65534 AND NOT uids.Exist(bits[0]) THEN 
       TRY KillAllChildren(CInt(bits[1]))
       IF Utils.Debugging THEN PRINT "\\\\\\Killed: " & bits[1] & " of " & bits[0]
    ENDIF 
  NEXT 
   
END

Reply to: