Why does killing a task with taskkill return so many errors?

385 views Asked by At

I am just wondering, why does my code return so many errors, even after successfully killing a task.

import subprocess
subprocess.call("taskkill /IM chrome.exe")

This is what I did, and it does the job. But I wish to know the reasoning behind the output i get:

SUCCESS: Sent termination signal to the process "chrome.exe" with PID 10212.
SUCCESS: Sent termination signal to the process "chrome.exe" with PID 11908.
SUCCESS: Sent termination signal to the process "chrome.exe" with PID 3492. 
ERROR: The process "chrome.exe" with PID 13116 could not be terminated.     
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 11736 could not be terminated.     
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 2156 could not be terminated.      
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 9192 could not be terminated.      
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 15884 could not be terminated.     
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 11344 could not be terminated.     
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 4700 could not be terminated.      
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 14548 could not be terminated.     
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 6224 could not be terminated.      
Reason: This process can only be terminated forcefully (with /F option).    
ERROR: The process "chrome.exe" with PID 7000 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 3564 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 16072 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 15856 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 11308 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 524 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).
ERROR: The process "chrome.exe" with PID 10188 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

I get similar results with os.

import os
os.system("taskkill /im chrome.exe /f")

I read into the docs of taskkill a little, but didn't really find the answer i was looking for. Could anyone explain, why this is happening?

1

There are 1 answers

0
Devyl On BEST ANSWER

personnaly i prefer

import os
import signal
import psutil

def get_pids(name):
    for proc in psutil.process_iter():
        if name in proc.name():
           yield proc.pid
for pid in get_pids("chrome"):
    os.kill(pid, signal.SIGTERM) #or signal.SIGKILL