Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Linux script to get most expensive processes Date: Tue, 04 Aug 2015 23:30:48 +0200 Organization: Decebal Computing Lines: 52 Message-ID: <87pp32hhbr.fsf@Equus.decebal.nl> References: <87twsehkmk.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="13802"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+cgMCbM790ZBzIDfBhLUa0g1qIbClIDow=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:DKzC/7c0Devcj8HY3kZIgAT5k+k= sha1:LTOxGdD2i47IA0zxfHlXDhnanKg= Xref: csiph.com comp.lang.python:94977 On Tuesday 4 Aug 2015 22:52 CEST, Emile van Sebille wrote: > On 8/4/2015 1:19 PM, 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. > >> Is this a reasonable way to do this? Getting the parameter is done >> quit simple, but I did not think fancy was necessary here. > > > My platform shows as linux2 and it worked fine for me when checking > for that. I heard that that was possible also, but none of my systems gives this. I should change it. I was also thinking about posix systems, but the Linux ps does more as others, so I did not do that. I amended the code to work with linux and linux2: ======================================================================== accepted_params = { 'pcpu', 'rss', 'size', 'time', 'vsize', } accepted_platforms = { 'linux', 'linux2', } current_platform = sys.platform max_line_length = 200 no_of_lines = 8 # One extra for the heading is_good_platform = False for platform in accepted_platforms: if platform == current_platform: is_good_platform = True break if not is_good_platform: raise Exception('Got an incompatiple platform: {0}'. format(current_platform)) ======================================================================== -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof