I have a python script that tests if any firefox processes are running on my windows machine and then kills them:
import os, subprocess
running_processes = subprocess.check_output('tasklist', shell = True)
if "firefox.exe" in running_processes:
os.system("TASKKILL /F /IM firefox.exe")
I would like to only kill firefox processes whose memory consumption and CPU usage are constant over time (i.e. do not change within a certain interval).
How to kill windows processes with constant memory and CPU usage in python?
My current approach to kill stale windows processes involves a function that returns a tuple of (process PID / process memory consumption) for every running process.
If the tuple doesn't change between two consecutive function calls, I kill the process by PID. This approach has one limitation: The to-be-monitored process needs to be known (named) in advance.
In an endless loop, I call this function consecutively with a short break in between. In case any process-specific tuple is identical across runs, the memory consumption of the respective process did not change - this process can now be killed by PID.