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


Groups > comp.lang.python > #94975

Linux script to get most expensive processes

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Linux script to get most expensive processes
Date Tue, 04 Aug 2015 22:19:31 +0200
Organization Decebal Computing
Lines 69
Message-ID <87twsehkmk.fsf@Equus.decebal.nl> (permalink)
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="29392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++5/0XecU2OtoK207Pqts8umNjcMPUQD8="
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
X-Homepage http://www.decebal.nl/
Cancel-Lock sha1:TZz1el66LkhZfa0eyvuJ3j4n5Vs= sha1:GBm7m0pz2tckmLacrbiVkUljpQ4=
Xref csiph.com comp.lang.python:94975

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web