- #!/usr/bin/python2.6
- import os,sys,re,threading
- platform = sys.platform
- class show_ps():
- def __init__(self,command,name):
- self.comm=command
- self.ps_name=name
- def show(self):
- if platform == 'linux2':
- lcmd = "ps -ef|grep -v grep|grep %s|awk '{print $2,$8}'"% (self.ps_name)
- date=os.popen(lcmd).readlines()
- outputlist = []
- for row in date:
- list = row.split()
- outputlist.append(list)
- return outputlist
- elif platform == 'win32':
- cmd = 'tasklist /FI "IMAGENAME eq %s" /NH'% (self.ps_name)
- data = os.popen(cmd).readlines()
- outputlist = []
- pid = []
- for row in data:
- string = row[0:35]
- list = string.split()
- print list
- outputlist.append(list)
- return outputlist
- class c_show_ps(show_ps):
- # def __init__(self,a):
- # super(c_show_ps,self)
- def mend(self):
- ps_list=self.show()
- new_list = []
- for i in ps_list:
- if len(i) > 0:
- tmp = {i[0]:i[1]}
- new_list.append(tmp)
- print new_list
- class killall():
- def __init__(self,command,name):
- self.comm = command
- self.ps_name = name
- def work(self):
- if platform == 'linux2':
- lcmd = "killall -9 %s"% (self.ps_name)
- os.popen(lcmd)
- print 'finished'
- elif platform == 'win32':
- cmd = 'taskkill /F /FI "IMAGENAME eq %s"'% (self.ps_name)
- os.popen(cmd)
- print 'finished'
- class kill():
- def __init__(self,command,id):
- self.comm=command
- self.ps_id=id
- def work(self):
- if platform == 'linux2':
- os.system( 'kill -9 %s'% (self.ps_id))
- print 'finished'
- elif platform == 'win32':
- for i in self.ps_id:
- os.system('taskkill /PID %s /F'% (i))
- print 'finished'
- class MyThread(threading.Thread):
- def __init__(self,comm,name):
- threading.Thread.__init__(self)
- self.comm = comm
- self.name = name
- def run(self):
- if platform == 'linux2':
- lcmd = 'ps -ef|grep -v grep|grep %s'% (self.name)
- #data = os.popen(lcmd).readlines()
- outputlist = []
- while True:
- outputlist = []
- lcmd = 'ps -ef|grep -v grep|grep %s'% (self.name)
- data = os.popen(lcmd).readlines()
- for row in data:
- list = row.split()
- outputlist.append(list)
- #print 1
- if len(outputlist) != 0:
- #a = 1
- #print '%s is running'% (self.name)
- continue
- elif len(outputlist) == 0:
- lcmd = '/etc/init.d/%s start'% (self.name)
- os.popen(lcmd)
- continue
- # if platform == 'win32':
- # while True:
- # cmd = 'tasklist /FI "IMAGENAME eq %s" /NH'% (self.name)
- # data = os.popen(cmd).readlines()
- # outputlist = []
- # for row in data:
- # outputlist = row
- # number = len(outputlist)
- # if number == 0:
- # os.system()
- class user_input():
- def __init__(self):
- pass
- def chk1(self,cmd,parems):
- b = c_show_ps( cmd, parems)
- b.mend()
- def chk2(self,cmd,parems):
- c = kill(cmd,parems)
- c.work()
- def chk3(self,cmd,parems):
- d = killall(cmd,parems)
- d.work()
- def thread(self,cmd,parems):
- t = MyThread(cmd,parems)
- t.setDaemon(True)
- t.start()
- while 1:
- info = raw_input('Enter your command:')
- infolist = info.split(' ',1)
- if infolist[0] == 'kill' and len(infolist[1].split()) >= 1:
- for i in infolist[1].split():
- p = re.compile('^\d+$')
- if p.match(i) != None and int(i)> 0:
- f = user_input()
- f.chk2(infolist[0],i)
- else:
- print "error"
- elif infolist[0] == 'ps' and len(infolist[1].split()) == 1:
- f = user_input()
- f.chk1(infolist[0],infolist[1])
- elif infolist[0] == 'killall' and len(infolist[1].split()) == 1:
- f = user_input()
- f.chk3(infolist[0],infolist[1])
- elif infolist[0] == 'watch':
- f = user_input()
- f.thread(infolist[0],infolist[1])
- elif info == 'exit':
- break
- else:
- print "unknow cmd"
用python写的一个跨平台处理进程的工具,比较粗糙,发出来,主要是为了记录学习的过程。
转载于:https://blog.51cto.com/vavahe/961428