#coding=utf-8import subPRocessclass FetchProcess(object): def __init__(self): p1 = subprocess.Popen(["ps", "-aux"], stdout=subprocess.PipE) # p2 = subprocess.Popen(["grep", "python3"], stdin=p1.stdout, stdout = subprocess.PIPE) self._output_temp = p1.communicate()[0].decode("utf-8").split("/n") #列表格式 self._output = self._parse(self._output_temp) # print("this is self._output:",self._output) @staticmethod def _parse(alist): result = [] for i in alist[1:]: temp = [] for j in i.split(" "): if j or j != "": temp.append(j) # print("this is temp",temp) result.append(temp) return result def get_pid(self,process_name): pid_list = [] for process in self._output: if process_name in process: pid = process[1] pid_list.append(pid) return pid_list def kill_process(self,process_name): if self.is_exist(process_name): pid_list = self.get_pid(process_name) for pid in pid_list: print("killing pid:",pid) subprocess.call("kill" + " -9 " + pid , shell = True) else: print("do not exist this thread,check your process name...") def is_exist(self,process_name): pid_list = self.get_pid(process_name) if len(pid_list) > 0: return True else: return False def start_process(self,process_name): pass def __exist__(self): passif __name__ == "__main__": myprocess = FetchProcess() pid_list = myprocess.kill_process('main_test.py')
新闻热点
疑难解答