Search code examples
pythonstartswithtaskkill

How to kill tasks which begin with string using python


I use the following commands to kill a task in Task Manager.

os.system("taskkill /f /im  " + ProgName)

It works fine for most tasks but I found one that it fails to kill.

CefSharp.BrowserSubprocess.exe

I think its because it has more than one period in the name but I am not sure. I tried multiple variations of the name.

CefSharp.BrowserSubprocess.exe
CefSharp.BrowserSubprocess
\"CefSharp.BrowserSubprocess.exe\"
\"CefSharp.BrowserSubprocess\"

Is there a way to kill all tasks that begin with a string (e.x. CefSharp*) using python3?

P.S. this is my very first question on the site, please forgive my ignorance of the subject and any mistakes in tagging or phrasing.


Solution

  • The Windows taskkill tool does take wildcards, so it should be possible to test:

    os.system("taskkill /F /IM CefSharp* /T")
    

    /T is used to kill all child processes.

    Or as follows:

    ProgName = "CefSharp*"
    os.system(f"taskkill /F /IM {ProgName} /T")