Python3写的小工具,windows下杀死进程
windows下如果杀掉某些进程挺麻烦的,用任务管理器操作繁琐,用cmd下输入taskkill /F /IM xxx输入的字符挺多的,也是挺麻烦的
我写了一个Python的小工具,可以杀掉进程
用法:运行脚本
>>进程名(几个关键字就可以),或者pid号
>>look 会刷新列表,并显示
脚本有两个文件
-------------------------winproc.py-------------------------------
import ctypes
import sys
TH32CS_SNAPPROCESS = 0x00000002
class PROCESSENTRY32(ctypes.Structure):
_fields_ = [("dwSize", ctypes.c_ulong),
("cntUsage", ctypes.c_ulong),
("th32ProcessID", ctypes.c_ulong),
("th32DefaultHeapID", ctypes.c_ulong),
("th32ModuleID", ctypes.c_ulong),
("cntThreads", ctypes.c_ulong),
("th32ParentProcessID", ctypes.c_ulong),
("pcPriClassBase", ctypes.c_ulong),
("dwFlags", ctypes.c_ulong),
("szExeFile", ctypes.c_char * 260)]
def getProcList():
CreateToolhelp32Snapshot = ctypes.windll.kernel32.CreateToolhelp32Snapshot
Process32First = ctypes.windll.kernel32.Process32First
Process32Next = ctypes.windll.kernel32.Process32Next
CloseHandle = ctypes.windll.kernel32.CloseHandle
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
pe32 = PROCESSENTRY32()
pe32.dwSize = ctypes.sizeof(PROCESSENTRY32)
if Process32First(hProcessSnap,ctypes.byref(pe32)) == False:
print("Failed getting first process.", file=sys.stderr)
return
while True:
yield pe32
if Process32Next(hProcessSnap,ctypes.byref(pe32)) == False:
break
CloseHandle(hProcessSnap)
def getChildPid(pid):
procList = getProcList()
for proc in procList:
if proc.th32ParentProcessID == pid:
yield proc.th32ProcessID
def killPid(pid):
childList = getChildPid(pid)
for childPid in childList:
killPid(childPid)
handle = ctypes.windll.kernel32.OpenProcess(1, False, pid)
ctypes.windll.kernel32.TerminateProcess(handle,0)
if __name__ =='__main__':
args = sys.argv
if len(args) >1 :
pid = int(args[1])
killPid(pid)
else:
procList = getProcList()
for proc in procList:
print(str(proc.szExeFile)+' '+str(proc.th32ParentProcessID) + ' '+str(proc.th32ProcessID))
-------------------------------主程序killtask.py------------------------------------------------
import getopt
import winproc #winproc就是同级目录下的winproc.py
procList2=[]
def lookproc():
procList = winproc.getProcList()
for proc in procList:
#print(proc.szExeFile.decode('gbk')+' '+str(proc.th32ParentProcessID) + ' '+str(proc.th32ProcessID))
print(proc.szExeFile.decode('gbk')+' '+str(proc.th32ProcessID))
procList2.append([proc.szExeFile.decode('gbk'),proc.th32ProcessID])
if __name__ =='__main__':
lookproc()
while True:
cmd = input('>>')
if cmd=='exit':
break
elif cmd=='look':
print('查看进程')
procList2=[]
lookproc()
elif cmd.isnumeric():
pid = int(cmd)
winproc.killPid(int(cmd))
for proc in procList2:
if pid == proc[1]:
print('结束进程:"'+proc[0]+'"\t'+str(pid))
else:
pid = 0
for proc in procList2:
#print('找到',proc[0],type(proc[0]))
if proc[0].find(cmd) != -1:
pid = proc[1]
winproc.killPid(pid)
print('结束进程:"'+proc[0]+'"\t'+str(pid))
if pid == 0:
print('没有找到"'+cmd+'"')
#args = sys.a
补充:Web开发 , Python ,