diff options
author | Chen Hanxiao <chenhanxiao@gmail.com> | 2016-05-04 11:12:29 +0800 |
---|---|---|
committer | Chen Hanxiao <chenhanxiao@gmail.com> | 2016-06-16 23:46:42 +0800 |
commit | 9cbcdbae6ee6be182b520ca85d96a1ec84a9770e (patch) | |
tree | 6e2e0b28350fe6d8668697606f44cf2ec5ea3bf6 /scripts/ps.py | |
parent | 46c0a8bdd54811570e487c8c6bdaa4b8fa302cd4 (diff) | |
download | psutil-9cbcdbae6ee6be182b520ca85d96a1ec84a9770e.tar.gz |
add STAT for ps.py
Add STAT to display state of a process.
Signed-off-by: Chen Hanxiao <chenhanxiao@gmail.com>
Diffstat (limited to 'scripts/ps.py')
-rwxr-xr-x | scripts/ps.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/scripts/ps.py b/scripts/ps.py index 9143a557..8aa3d3f4 100755 --- a/scripts/ps.py +++ b/scripts/ps.py @@ -17,17 +17,37 @@ import time import psutil +PROC_STATUSES_RAW = { + psutil.STATUS_RUNNING: "R", + psutil.STATUS_SLEEPING: "S", + psutil.STATUS_DISK_SLEEP: "D", + psutil.STATUS_STOPPED: "T", + psutil.STATUS_TRACING_STOP: "t", + psutil.STATUS_ZOMBIE: "Z", + psutil.STATUS_DEAD: "X", + psutil.STATUS_WAKING: "WA", + psutil.STATUS_IDLE: "I", + 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 %7s %s" + templ = "%-10s %5s %4s %4s %7s %7s %-13s %-5s %5s %7s %s" attrs = ['pid', 'cpu_percent', 'memory_percent', 'name', 'cpu_times', - 'create_time', 'memory_info'] + 'create_time', 'memory_info', 'status'] if os.name == 'posix': attrs.append('uids') attrs.append('terminal') print(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", - "START", "TIME", "COMMAND")) + "STAT", "START", "TIME", "COMMAND")) for p in psutil.process_iter(): try: pinfo = p.as_dict(attrs, ad_value='') @@ -64,6 +84,7 @@ def main(): int(pinfo['memory_info'].rss / 1024) or '?' memp = pinfo['memory_percent'] and \ round(pinfo['memory_percent'], 1) or '?' + status = PROC_STATUSES_RAW.get(pinfo['status'], pinfo['status']) print(templ % ( user[:10], pinfo['pid'], @@ -72,6 +93,7 @@ def main(): vms, rss, pinfo.get('terminal', '') or '?', + status, ctime, cputime, pinfo['name'].strip() or '?')) |