63
|
1 #! /usr/bin/env python
|
|
2 """find and kill processes
|
|
3 """
|
|
4 import pexpect
|
|
5 import re
|
|
6 import sys
|
|
7 import os
|
|
8
|
|
9 def main(proc):
|
|
10 child = pexpect.spawn('ps aucxwww')
|
|
11 child.setwinsize(100,300)
|
|
12 child.expect(pexpect.EOF)
|
|
13
|
|
14 pslist = child.before.splitlines()
|
|
15 # separate string by space
|
|
16 fmt = re.split(' +', pslist[0])
|
|
17
|
|
18 if fmt[-1] == '':
|
|
19 fmt.pop()
|
|
20 if fmt[0] == '':
|
|
21 fmt.pop(0)
|
|
22 lenfmt = len(fmt)
|
|
23
|
|
24 # indexing, key is format of output of ps command, value is number of list
|
|
25 index = dict([(k,v) for (k,v) in zip(fmt, range(0,lenfmt))])
|
|
26
|
|
27 # find ".*<processname>.*" in 'COMMAND' part
|
|
28 pars = re.compile(".*"+proc+".*")
|
|
29
|
|
30 for l in pslist[1:]:
|
|
31 elm = re.split(' +',l ,lenfmt)
|
|
32 cmd = elm[index['COMMAND']]
|
|
33 result = pars.match(cmd)
|
|
34
|
|
35 if (result is not None):
|
|
36 os.system('kill -KILL ' + elm[index['PID']])
|
|
37 print 'stop', elm[index['PID']], ':', cmd
|
|
38
|
|
39
|
|
40 if __name__== "__main__":
|
|
41 if (len(sys.argv) != 2):
|
|
42 print "Usage : %s <process_name>" % sys.argv[0]
|
|
43 sys.exit(1)
|
|
44
|
|
45 main(sys.argv[1])
|
|
46
|