1. #!/usr/bin/python2.6 
  2. import os,sys,re,threading 
  3.  
  4. platform = sys.platform 
  5.  
  6.  
  7. class show_ps(): 
  8.     def __init__(self,command,name): 
  9.         self.comm=command 
  10.         self.ps_name=name 
  11.  
  12.     def show(self): 
  13.  
  14.         if platform == 'linux2'
  15.             lcmd = "ps -ef|grep -v grep|grep %s|awk '{print $2,$8}'"% (self.ps_name) 
  16.             date=os.popen(lcmd).readlines() 
  17.             outputlist = [] 
  18.             for row in date: 
  19.                 list = row.split() 
  20.                 outputlist.append(list) 
  21.             return outputlist 
  22.  
  23.         elif platform == 'win32'
  24.             cmd = 'tasklist /FI "IMAGENAME eq %s" /NH'% (self.ps_name) 
  25.             data = os.popen(cmd).readlines() 
  26.             outputlist = [] 
  27.             pid = [] 
  28.             for row in data: 
  29.                 string = row[0:35
  30.                 list = string.split() 
  31.                 print list 
  32.                 outputlist.append(list) 
  33.             return outputlist 
  34.  
  35.  
  36. class c_show_ps(show_ps): 
  37. #    def __init__(self,a): 
  38. #        super(c_show_ps,self) 
  39.  
  40.     def mend(self): 
  41.  
  42.         ps_list=self.show() 
  43.         new_list = [] 
  44.         for i in ps_list: 
  45.             if len(i) > 0
  46.                 tmp = {i[0]:i[1]} 
  47.                 new_list.append(tmp) 
  48.         print new_list 
  49.  
  50.  
  51. class killall(): 
  52.     def __init__(self,command,name): 
  53.         self.comm = command 
  54.         self.ps_name = name 
  55.     def work(self): 
  56.  
  57.         if platform == 'linux2'
  58.             lcmd = "killall -9 %s"% (self.ps_name) 
  59.             os.popen(lcmd) 
  60.             print 'finished' 
  61.  
  62.         elif platform == 'win32'
  63.             cmd = 'taskkill /F /FI "IMAGENAME eq %s"'% (self.ps_name) 
  64.             os.popen(cmd) 
  65.             print 'finished' 
  66.  
  67. class kill(): 
  68.     def __init__(self,command,id): 
  69.         self.comm=command 
  70.         self.ps_id=id 
  71.  
  72.     def work(self): 
  73.         if platform == 'linux2'
  74.             os.system( 'kill -9 %s'% (self.ps_id)) 
  75.             print 'finished' 
  76.  
  77.         elif platform == 'win32'
  78.             for i in self.ps_id: 
  79.                 os.system('taskkill /PID %s /F'% (i)) 
  80.                 print 'finished' 
  81.  
  82.  
  83. class MyThread(threading.Thread): 
  84.     def __init__(self,comm,name): 
  85.         threading.Thread.__init__(self
  86.         self.comm = comm 
  87.         self.name = name 
  88.  
  89.     def run(self): 
  90.         if platform == 'linux2'
  91.             lcmd = 'ps -ef|grep -v grep|grep %s'% (self.name) 
  92.            #data = os.popen(lcmd).readlines() 
  93.             outputlist = [] 
  94.             while True
  95.                 outputlist = [] 
  96.                 lcmd = 'ps -ef|grep -v grep|grep %s'% (self.name) 
  97.                 data = os.popen(lcmd).readlines() 
  98.                 for row in data: 
  99.                     list = row.split() 
  100.                     outputlist.append(list) 
  101.                     #print 1 
  102.                 if len(outputlist) != 0
  103.                     #a = 1 
  104.                     #print '%s is running'% (self.name) 
  105.                     continue 
  106.                 elif len(outputlist) == 0
  107.                     lcmd = '/etc/init.d/%s start'% (self.name) 
  108.                     os.popen(lcmd) 
  109.                     continue 
  110.  
  111.  
  112. #        if platform == 'win32': 
  113. #            while True: 
  114. #                cmd = 'tasklist /FI "IMAGENAME eq %s" /NH'% (self.name) 
  115. #                data = os.popen(cmd).readlines() 
  116. #                outputlist = [] 
  117. #                for row in data: 
  118. #                    outputlist = row 
  119. #                    number = len(outputlist) 
  120. #                    if number == 0: 
  121. #                        os.system() 
  122.  
  123.  
  124. class user_input(): 
  125.     def __init__(self): 
  126.         pass 
  127.     def chk1(self,cmd,parems): 
  128.         b = c_show_ps( cmd, parems) 
  129.         b.mend() 
  130.  
  131.     def chk2(self,cmd,parems): 
  132.         c = kill(cmd,parems) 
  133.         c.work() 
  134.  
  135.     def chk3(self,cmd,parems): 
  136.         d = killall(cmd,parems) 
  137.         d.work() 
  138.  
  139.     def thread(self,cmd,parems): 
  140.         t = MyThread(cmd,parems) 
  141.         t.setDaemon(True
  142.         t.start() 
  143.  
  144. while 1
  145.     info = raw_input('Enter your command:'
  146.     infolist = info.split(' ',1
  147.  
  148.     if infolist[0] == 'kill' and len(infolist[1].split()) >= 1
  149.         for i in infolist[1].split(): 
  150.             p = re.compile('^\d+$'
  151.             if p.match(i) != None and int(i)> 0
  152.                 f = user_input() 
  153.                 f.chk2(infolist[0],i) 
  154.             else
  155.                 print "error" 
  156.  
  157.     elif infolist[0] == 'ps' and len(infolist[1].split()) == 1
  158.         f = user_input() 
  159.         f.chk1(infolist[0],infolist[1]) 
  160.  
  161.     elif infolist[0] == 'killall' and len(infolist[1].split()) == 1
  162.         f = user_input() 
  163.         f.chk3(infolist[0],infolist[1]) 
  164.  
  165.     elif infolist[0] == 'watch'
  166.         f = user_input() 
  167.         f.thread(infolist[0],infolist[1]) 
  168.  
  169.     elif info == 'exit'
  170.         break 
  171.  
  172.     else
  173.         print "unknow cmd" 
 
用python写的一个跨平台处理进程的工具,比较粗糙,发出来,主要是为了记录学习的过程。