Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94975 > unrolled thread

Linux script to get most expensive processes

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-08-04 22:19 +0200
Last post2015-08-06 06:06 +0200
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Linux script to get most expensive processes Cecil Westerhof <Cecil@decebal.nl> - 2015-08-04 22:19 +0200
    Re: Linux script to get most expensive processes Emile van Sebille <emile@fenx.com> - 2015-08-04 13:52 -0700
      Re: Linux script to get most expensive processes Cecil Westerhof <Cecil@decebal.nl> - 2015-08-04 23:30 +0200
        Re: Linux script to get most expensive processes MRAB <python@mrabarnett.plus.com> - 2015-08-04 23:00 +0100
          Re: Linux script to get most expensive processes Cecil Westerhof <Cecil@decebal.nl> - 2015-08-05 01:14 +0200
        Re: Linux script to get most expensive processes Emile van Sebille <emile@fenx.com> - 2015-08-04 15:12 -0700
          Re: Linux script to get most expensive processes Cecil Westerhof <Cecil@decebal.nl> - 2015-08-05 01:17 +0200
    Re: Linux script to get most expensive processes Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-08-05 12:56 +0200
    Re: Linux script to get most expensive processes Laura Creighton <lac@openend.se> - 2015-08-06 06:06 +0200

#94975 — Linux script to get most expensive processes

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-08-04 22:19 +0200
SubjectLinux script to get most expensive processes
Message-ID<87twsehkmk.fsf@Equus.decebal.nl>
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.

========================================================================
#!/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()
    for line in output[:no_of_lines]:
        print(line.decode('utf-8'))


accepted_params     = {
    'pcpu',
    'rss',
    'size',
    'time',
    'vsize',
}
current_platform    = sys.platform
max_line_length     = 200
needed_platform     = 'linux'
no_of_lines         = 8                     # One extra for the heading

if current_platform != needed_platform:
    raise Exception('Needs a {0} platform, got {1} platform'.
                    format(needed_platform, current_platform))
no_of_parameters = len(sys.argv) - 1
if no_of_parameters == 0:
    to_check = 'pcpu'
elif no_of_parameters == 1:
    to_check = sys.argv[1]
else:
    raise Exception('Too many arguments')

if (to_check != 'all') and not(to_check in accepted_params):
    raise Exception('Used illegal parameter: {0}'.format(to_check))

if to_check == 'all':
    for param in sorted(accepted_params):
        give_output(param)
        print()
else:
    give_output(to_check)
========================================================================

Is this a reasonable way to do this? Getting the parameter is done
quit simple, but I did not think fancy was necessary here.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#94976

FromEmile van Sebille <emile@fenx.com>
Date2015-08-04 13:52 -0700
Message-ID<mailman.1218.1438721578.3674.python-list@python.org>
In reply to#94975
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.
<snip>
> 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.

Emile



[toc] | [prev] | [next] | [standalone]


#94977

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-08-04 23:30 +0200
Message-ID<87pp32hhbr.fsf@Equus.decebal.nl>
In reply to#94976
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.
> <snip>
>> 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

[toc] | [prev] | [next] | [standalone]


#94978

FromMRAB <python@mrabarnett.plus.com>
Date2015-08-04 23:00 +0100
Message-ID<mailman.1219.1438725612.3674.python-list@python.org>
In reply to#94977
On 2015-08-04 22:30, Cecil Westerhof wrote:
> 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.
>> <snip>
>>> 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))
> ========================================================================
>
Doesn't that 'for' loop do the same as:

     is_good_platform = current_platform in accepted_platforms

?

[toc] | [prev] | [next] | [standalone]


#94982

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-08-05 01:14 +0200
Message-ID<87k2tahcir.fsf@Equus.decebal.nl>
In reply to#94978
On Wednesday  5 Aug 2015 00:00 CEST, MRAB wrote:

>> 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))
>> ========================================================================
>>
> Doesn't that 'for' loop do the same as:
>
> is_good_platform = current_platform in accepted_platforms

You are right. Not very smart. Especially because I use the ‘in’ a
little later.

It now is:
========================================================================
if not current_platform in accepted_platforms:
    raise Exception('Got an incompatiple platform: {0}'.
                    format(current_platform))
========================================================================

And I do not need is_good_platform anymore.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#94980

FromEmile van Sebille <emile@fenx.com>
Date2015-08-04 15:12 -0700
Message-ID<mailman.1220.1438726343.3674.python-list@python.org>
In reply to#94977
On 8/4/2015 2:30 PM, Cecil Westerhof wrote:
> On Tuesday  4 Aug 2015 22:52 CEST, Emile van Sebille wrote:

>> 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.

You could also use the platform module:


 >>> import platform
 >>> platform.system()
'Linux'
 >>>


Emile

[toc] | [prev] | [next] | [standalone]


#94981

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-08-05 01:17 +0200
Message-ID<87fv3yhcdo.fsf@Equus.decebal.nl>
In reply to#94980
On Wednesday  5 Aug 2015 00:12 CEST, Emile van Sebille wrote:

> On 8/4/2015 2:30 PM, Cecil Westerhof wrote:
>> On Tuesday  4 Aug 2015 22:52 CEST, Emile van Sebille wrote:
>
>>> 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.
>
> You could also use the platform module:
>
>
>>>> import platform
>>>> platform.system()
> 'Linux'

I already use sys, so I think sys.platform is a little better. ;-)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#95011

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-08-05 12:56 +0200
Message-ID<3702826.bDmuSmBusV@PointedEars.de>
In reply to#94975
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.

[toc] | [prev] | [next] | [standalone]


#95061

FromLaura Creighton <lac@openend.se>
Date2015-08-06 06:06 +0200
Message-ID<mailman.1264.1438833978.3674.python-list@python.org>
In reply to#94975
If you are running this script with Python 2 write:

   if sys.platform.startswith('linux'):

to handle the case where you get linux or linux2 (and a few other weird
things some embedded systems give you ...)

Right now I think every linux system returns linux for Python 3, so it
is less of an issue (now, anyway).

Laura

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web