windows 批量杀进程
1 import psutil 2 from subprocess import Popen, PIPE 3 4 process_name ="bsmr.exe,fxclient.exe,FxTerm.exe,Xshell.exe,Foxmail.exe" 5 def getoutput(command): 6 p = Popen(command, stdin=PIPE, stderr=PIPE, shell=True) 7 out, err = p.communicate() 8 if err: 9 print('command exec error: [{}]'.format(command)) 10 print(err) 11 else: 12 return out 13 14 15 def kill_process(): 16 p = psutil.pids() 17 succeed_name = [] 18 succeed_process = [] 19 failed_process = [] 20 for i in psutil.pids(): 21 info = psutil.Process(i) 22 succeed_name.append(info.name()) 23 for i in process_name.split(','): 24 if i in succeed_name: 25 #print("进程名称为:{}".format(i)) 26 print("开始执行kill命令") 27 command = 'taskkill /IM {} /F'.format(i) 28 print(command) 29 getoutput(command) 30 succeed_process.append(i) 31 else: 32 print("当前进程不存在:{}".format(i)) 33 failed_process.append(i) 34 print("kill进程失败,当前kill掉进程为:{}, 未kill进程为:{}".format(succeed_process, failed_process)) 35 36 37 if __name__ == '__main__': 38 kill_process()