Linux script to get most expensive processes

Thomas 'PointedEars' Lahn PointedEars at web.de
Wed Aug 5 12:56:02 CEST 2015


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.


More information about the Python-list mailing list