You can use the psutil library to get the percentage of processor processes and ultimately kill them. This library works with python from 2.4 to 3.3 (and PyPy), on Linux, Windows, Solaris, FreeBSD and OS X, both on 32 and 64 bits.
The following (unverified) code should do what you want:
gnome_panel_procs = [] for process in psutil.process_iter(): # I assume the gnome-panel processes correctly set their name # eventually you could use process.cmdline instead if process.name == 'gnome-panel': gnome_panel_procs.append(process) for proc in gnome_panel_procs: for _ in range(60): # check cpu percentage over 1 second if proc.get_cpu_percent(1) < 80 or not proc.is_running(): # less than 80% of cpu or process terminated break else: # process used 80% of cpu for over 1 minute proc.kill()
Note: calling is_running() prevents problems with is_running() pid , which can happen in other proposed solutions (albeit with very little chance).
If you want to check if the process was started more than one minute ago, and at that moment more than 80% of the CPU is used, then you can use something simpler:
import time import psutil for proc in psutil.process_iter(): if proc.name == 'gnome-panel' and time.time() - proc.create_time > 1: if proc.get_cpu_percent() > 80: proc.kill()
This will kill any gnome-panel process, although during the last minute it has not used a lot of CPUs, but only in the last few seconds.
source share