I'm looking for a pretty-print of an Ubuntu process with python. I'm getting to know psutil but any idea if there is something better?

182 views Asked by At

I've looked about the psutil library (https://code.google.com/archive/p/psutil/wikis/Documentation.wiki) and got some useful things like

```

PROCESS_ATTRS = ['username', 'cpu_num', 'num_ctx_switches', 'pid', 'memory_full_info', 'connections', 'cmdline', 'create_time', 'ionice', 'num_fds', 'memory_maps', 'cpu_percent', 'terminal', 'ppid', 'cwd', 'nice', 'status', 'cpu_times', 'io_counters', 'memory_info', 'threads', 'open_files', 'uids', 'num_threads', 'exe', 'name', 'gids', 'cpu_affinity', 'memory_percent', 'environ']
for proc in psutil.process_iter():
    print("Querying process: %s [%s]" % (proc.name(), proc.pid))
    print proc.as_dict(attrs=PROCESS_ATTRS)

```

Is there any other library for this, or any useful methods about psutil that I'm missing?

Thanks in advance!

1

There are 1 answers

0
Giampaolo RodolĂ  On
>>> import psutil
>>> from pprint import pprint as pp
>>> pp([p.info for p in psutil.process_iter(attrs=('pid', 'name'))])
[{'name': 'systemd', 'pid': 1},
 {'name': 'kthreadd', 'pid': 2},
 {'name': 'ksoftirqd/0', 'pid': 3},
 {'name': 'kworker/0:0H', 'pid': 5},
 {'name': 'rcu_sched', 'pid': 7},
 {'name': 'rcu_bh', 'pid': 8},
 {'name': 'migration/0', 'pid': 9},
 {'name': 'watchdog/0', 'pid': 10},
 {'name': 'watchdog/1', 'pid': 11},
 {'name': 'migration/1', 'pid': 12},
 {'name': 'ksoftirqd/1', 'pid': 13},
 ...
]

More examples here: http://psutil.readthedocs.io/en/latest/#filtering-and-sorting-processes

Note: the doc you're using is ancient. psutil is now hosted on github.