How to kill a specific process using% cpu in a given time in python on linux?

I have this script in python on linux that deploys vnc locally, does some kind of graphic job on this vnc screen, and kills vnc. Sometimes, after shutdown, a process called gnome-panel freezes and stays with 100% processor utilization. Then I need to go through the putty and kill all these processes manually (sometimes there are a lot of them). I would like to add some lines to my python script when it finishes its task, which will not only kill vnc (it does it already), but also kill the gnome-panel if it consumes a certain amount of CPU for a given period of time. I can't just kill all the gnome panels, as some of them work fine (im deploying 4 vnc screens at the same time).

I need this condition in python:

if the process name is a gnome panel and consumes more than 80% of the processor and runs for more than 1 minute, destroy the process identifier

Thank you!

+4
source share
4 answers

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.

+2
source
 import os os.system(' ps aux| grep gnome-panel | awk \'{if($3>80) print $2}\' |xargs kill -9 ') 
+1
source

ps aux | grep 'gnome-panel' | awk '{if ($ 3> 80) print $ 2}' | xargs kill -9

0
source

It looks like this has already been implemented here to get ps aux results

 ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0] processes = ps.split('\n') # this specifies the number of splits, so the splitted lines # will have (nfields+1) elements nfields = len(processes[0].split()) - 1 processes = [row.split(None, nfields) for row in processes[1:]] 

Example:

 ['greg', '6359', '0.0', '0.1', '16956', '8868', 'pts/3', 'S+', '01:40', '0:00', 'python'] 

Then you can finish by going through all the processes

 for process in processes: if "gnome-terminal" in process[-1] \ and float(process[2]) > 0.8 \ and int(process[-2].split(":")[-1]) > 1: subprocess.call(["kill", "-9", process[0]]) 

I'm sure there is a less hacky way to do this, though

0
source

Source: https://habr.com/ru/post/1499088/


All Articles