Path: csiph.com!au2pb.net!feeder.erje.net!1.eu.feeder.erje.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.python Subject: Re: Linux script to get most expensive processes Date: Wed, 05 Aug 2015 12:56:02 +0200 Organization: PointedEars Software (PES) Lines: 62 Message-ID: <3702826.bDmuSmBusV@PointedEars.de> References: <87twsehkmk.fsf@Equus.decebal.nl> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1438772388 20573 eJwNxMERACEIA8CWEEiQcjKi/Zdwt49FcPFUEkw8vLAAoKD9l66POmab/OzGwC5WydndyvH5AASWEG8= (5 Aug 2015 10:59:48 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 5 Aug 2015 10:59:48 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwNx8cRwEAIBLCWiIsphyH0X8JZP7mC0WFwmJ8fevKK6l8xhc582nF2saO1FoGppeWRsxTNJPYGidiAmx9k1xVc Cancel-Lock: sha1:GJmsN79yKhpOv//M6i/3CT+4X9Q= X-NNTP-Posting-Host: eJwNwYEBwCAIA7CXYJTCzkGF/0/QxI3KHaATPm+fU/IYzHMUaC5K9Hx6pFGRhW2TyUpd/wUTHhDJ Xref: csiph.com comp.lang.python:95011 Cecil Westerhof wrote: > Under Linux I like to get the most expensive processes. The two most > useful commands are: > ps -eo pid,user,pcpu,args --sort=-pcpu > and: > ps -eo pid,user,pcpu,args --sort=-vsize > > In my case I am only interested in the seven most expensive processes. > For this I wrote the following script. Don’t. Use ps -eo pid,user,pcpu,args --sort=-pcpu | head -n 8 or ps -eo pid,user,pcpu,args --sort=-pcpu | sed -n '2,8p' and the like instead. (procps ps(1) also has an output modifier to omit the headers, but I could not get that to work with the sorting just now. [Thanks for pointing out the “--sort” option of *procps* ps(1) 3.3.10. I was not aware of it, and had used $ alias cpu alias cpu='ps -ww aux | sort -nk3 | tail' instead.] > ======================================================================== > #!/usr/bin/env python3 > > import subprocess > import sys > > > def give_output(param): > output = subprocess.check_output(([ > 'ps', > '--columns={0}' .format(max_line_length), > '-eo', > 'pid,user,start_time,{0},args'.format(param), > '--sort=-{0}' .format(param) > ])).splitlines() > […] > ======================================================================== > > Is this a reasonable way to do this? No. > Getting the parameter is done quit[e] simple, but I did not think fancy > was necessary here. It is not. -- PointedEars Twitter: @PointedEars2 Please do not cc me. / Bitte keine Kopien per E-Mail.