annotate StopProcess.py @ 73:ad401b7c97bb

fix warnings
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 23 Oct 2009 14:16:59 +0900
parents b342dc9b52eb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63
b342dc9b52eb add document
axmo
parents:
diff changeset
1 #! /usr/bin/env python
b342dc9b52eb add document
axmo
parents:
diff changeset
2 """find and kill processes
b342dc9b52eb add document
axmo
parents:
diff changeset
3 """
b342dc9b52eb add document
axmo
parents:
diff changeset
4 import pexpect
b342dc9b52eb add document
axmo
parents:
diff changeset
5 import re
b342dc9b52eb add document
axmo
parents:
diff changeset
6 import sys
b342dc9b52eb add document
axmo
parents:
diff changeset
7 import os
b342dc9b52eb add document
axmo
parents:
diff changeset
8
b342dc9b52eb add document
axmo
parents:
diff changeset
9 def main(proc):
b342dc9b52eb add document
axmo
parents:
diff changeset
10 child = pexpect.spawn('ps aucxwww')
b342dc9b52eb add document
axmo
parents:
diff changeset
11 child.setwinsize(100,300)
b342dc9b52eb add document
axmo
parents:
diff changeset
12 child.expect(pexpect.EOF)
b342dc9b52eb add document
axmo
parents:
diff changeset
13
b342dc9b52eb add document
axmo
parents:
diff changeset
14 pslist = child.before.splitlines()
b342dc9b52eb add document
axmo
parents:
diff changeset
15 # separate string by space
b342dc9b52eb add document
axmo
parents:
diff changeset
16 fmt = re.split(' +', pslist[0])
b342dc9b52eb add document
axmo
parents:
diff changeset
17
b342dc9b52eb add document
axmo
parents:
diff changeset
18 if fmt[-1] == '':
b342dc9b52eb add document
axmo
parents:
diff changeset
19 fmt.pop()
b342dc9b52eb add document
axmo
parents:
diff changeset
20 if fmt[0] == '':
b342dc9b52eb add document
axmo
parents:
diff changeset
21 fmt.pop(0)
b342dc9b52eb add document
axmo
parents:
diff changeset
22 lenfmt = len(fmt)
b342dc9b52eb add document
axmo
parents:
diff changeset
23
b342dc9b52eb add document
axmo
parents:
diff changeset
24 # indexing, key is format of output of ps command, value is number of list
b342dc9b52eb add document
axmo
parents:
diff changeset
25 index = dict([(k,v) for (k,v) in zip(fmt, range(0,lenfmt))])
b342dc9b52eb add document
axmo
parents:
diff changeset
26
b342dc9b52eb add document
axmo
parents:
diff changeset
27 # find ".*<processname>.*" in 'COMMAND' part
b342dc9b52eb add document
axmo
parents:
diff changeset
28 pars = re.compile(".*"+proc+".*")
b342dc9b52eb add document
axmo
parents:
diff changeset
29
b342dc9b52eb add document
axmo
parents:
diff changeset
30 for l in pslist[1:]:
b342dc9b52eb add document
axmo
parents:
diff changeset
31 elm = re.split(' +',l ,lenfmt)
b342dc9b52eb add document
axmo
parents:
diff changeset
32 cmd = elm[index['COMMAND']]
b342dc9b52eb add document
axmo
parents:
diff changeset
33 result = pars.match(cmd)
b342dc9b52eb add document
axmo
parents:
diff changeset
34
b342dc9b52eb add document
axmo
parents:
diff changeset
35 if (result is not None):
b342dc9b52eb add document
axmo
parents:
diff changeset
36 os.system('kill -KILL ' + elm[index['PID']])
b342dc9b52eb add document
axmo
parents:
diff changeset
37 print 'stop', elm[index['PID']], ':', cmd
b342dc9b52eb add document
axmo
parents:
diff changeset
38
b342dc9b52eb add document
axmo
parents:
diff changeset
39
b342dc9b52eb add document
axmo
parents:
diff changeset
40 if __name__== "__main__":
b342dc9b52eb add document
axmo
parents:
diff changeset
41 if (len(sys.argv) != 2):
b342dc9b52eb add document
axmo
parents:
diff changeset
42 print "Usage : %s <process_name>" % sys.argv[0]
b342dc9b52eb add document
axmo
parents:
diff changeset
43 sys.exit(1)
b342dc9b52eb add document
axmo
parents:
diff changeset
44
b342dc9b52eb add document
axmo
parents:
diff changeset
45 main(sys.argv[1])
b342dc9b52eb add document
axmo
parents:
diff changeset
46