summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 19:25:05 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2019-04-05 19:25:05 +0200
commit279f6e4219eda6af2e1d05c762b022c5d7e10456 (patch)
tree7893fd890c45d5de35b51f0e5bd358e709818eea
parent90135fa7fa63a0eadd318374d455c04565ce1d8e (diff)
downloadpsutil-279f6e4219eda6af2e1d05c762b022c5d7e10456.tar.gz
improve ps.py script
-rwxr-xr-xscripts/ps.py51
1 files changed, 24 insertions, 27 deletions
diff --git a/scripts/ps.py b/scripts/ps.py
index bf28a462..72c315a8 100755
--- a/scripts/ps.py
+++ b/scripts/ps.py
@@ -12,7 +12,6 @@ $ python scripts/ps.py
"""
import datetime
-import os
import time
import psutil
@@ -32,25 +31,20 @@ PROC_STATUSES_RAW = {
psutil.STATUS_LOCKED: "L",
psutil.STATUS_WAITING: "W",
}
-
if hasattr(psutil, 'STATUS_WAKE_KILL'):
PROC_STATUSES_RAW[psutil.STATUS_WAKE_KILL] = "WK"
-
if hasattr(psutil, 'STATUS_SUSPENDED'):
PROC_STATUSES_RAW[psutil.STATUS_SUSPENDED] = "V"
def main():
today_day = datetime.date.today()
- templ = "%-10s %5s %4s %4s %7s %7s %-13s %-5s %5s %7s %s"
+ templ = "%-10s %5s %5s %5s %7s %7s %5s %-5s %5s %7s %s"
attrs = ['pid', 'cpu_percent', 'memory_percent', 'name', 'cpu_times',
- 'create_time', 'memory_info', 'status']
- if os.name == 'posix':
- attrs.append('uids')
- attrs.append('terminal')
- print(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY",
+ 'create_time', 'memory_info', 'status', 'nice', 'username']
+ print(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "NICE",
"STAT", "START", "TIME", "COMMAND"))
- for p in psutil.process_iter(attrs, ad_value=''):
+ for p in psutil.process_iter(attrs, ad_value=None):
pinfo = p.info
if pinfo['create_time']:
ctime = datetime.datetime.fromtimestamp(pinfo['create_time'])
@@ -60,28 +54,30 @@ def main():
ctime = ctime.strftime("%b%d")
else:
ctime = ''
- cputime = time.strftime("%M:%S",
- time.localtime(sum(pinfo['cpu_times'])))
- try:
- user = p.username()
- except KeyError:
- if psutil.POSIX:
- if pinfo['uids']:
- user = str(pinfo['uids'].real)
- else:
- user = ''
- else:
- raise
- except psutil.Error:
- user = ''
- if psutil.WINDOWS and '\\' in user:
+ if pinfo['cpu_times']:
+ cputime = time.strftime("%M:%S",
+ time.localtime(sum(pinfo['cpu_times'])))
+ else:
+ cputime = ''
+
+ user = pinfo['username']
+ if not user and psutil.POSIX:
+ try:
+ user = p.uids()[0]
+ except psutil.Error:
+ pass
+ if user and psutil.WINDOWS and '\\' in user:
user = user.split('\\')[1]
+ user = user[:9]
+
vms = bytes2human(pinfo['memory_info'].vms) if \
pinfo['memory_info'] is not None else ''
rss = bytes2human(pinfo['memory_info'].rss) if \
pinfo['memory_info'] is not None else ''
memp = round(pinfo['memory_percent'], 1) if \
pinfo['memory_percent'] is not None else ''
+ nice = int(pinfo['nice']) if pinfo['nice'] else ''
+ name = pinfo['name'] if pinfo['name'] else ''
status = PROC_STATUSES_RAW.get(pinfo['status'], pinfo['status'])
print(templ % (
@@ -91,11 +87,12 @@ def main():
memp,
vms,
rss,
- pinfo.get('terminal', '') or '',
+ nice,
status,
ctime,
cputime,
- pinfo['name'].strip() or '?'))
+ name
+ ))
if __name__ == '__main__':