Python3寫的小工具,windows下殺死進程

來源:互聯網
上載者:User

windows下如果殺掉某些進程挺麻煩的,用工作管理員操作繁瑣,用cmd下輸入taskkill /F /IM xxx輸入的字元挺多的,也是挺麻煩的

我寫了一個Python的小工具,可以殺掉進程.請使用最新的Python3.2.2,

Python3.2有Bug,如果你使用的3.2請在

cmd = input('>>')

下加一行

cmd=cmd.replace('\r') 

用法:運行指令碼

>>進程名(幾個關鍵字就可以),或者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=='':
            continue
        if cmd=='exit':
            break
        elif cmd=='look':
            print('查看進程')
            procList2=[]
            lookproc()
        elif cmd.isnumeric():
            pid = int(cmd)
            winproc.killPid(int(cmd))
            pid = 0
            for proc in procList2:
                if pid == proc[1]:
                    print('結束進程:"'+proc[0]+'"\t'+str(pid))
            if pid == 0:
                print('沒有找到PID='+cmd+'的進程')
        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.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))
    #        print(str(proc.

----------------------------------------------------------------------------------

兩個檔案放在一個目錄下雙擊主程式killtask.py

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.