Welcome, guest | Sign In | My Account | Store | Cart

A python script very loosely approximating pkill.

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python
import commands
import os,sys,string
import re

def pkill():
    if len(sys.argv) <= 1:
        print "usage: " + sys.argv[0] + " process_name"
        sys.exit(1)
    rip = sys.argv[1]
    me = commands.getoutput("whoami")
    p = commands.getoutput("ps -u %s -o fname -o pid" % me)
    
    ids = p.split("\n")
    for id in ids:
        if id.find(rip) < 0:
            continue
        regex = re.compile(r'(\d+).*',re.I)
        id = regex.sub(r'\1', id)
        print "killing: %s\n" % id
        commands.getoutput("kill -15 %s" % id)
        if commands.getoutput("ps -u %s -o fname -o pid" % me).find(id) > -1:
            print "slaughtering: %s\n" % id
            commands.getoutput("kill -9 %s" % id)
if __name__ == '__main__' :
    pkill()