I want to get all process information in a linux server.
PID, USER, CPU% , COMMAND ...
Exactly, I want to get these information in a python script. I tried
a,b = commands.getstatusoutput('top -n 1')
This gave me about 20 rows of results ordered by CPU%, but I want more, about 40 or 50 rows of results.
How should I do?
I use psutil as follow, for each proc, it need to wait 1 second. How can I get cpu_percent like top command quickly.
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'username', 'cpu_percent', 'name'])
pinfo['cpu_percent'] = proc.cpu_percent(interval=1)
if pinfo['cpu_percent'] > 0:
print(pinfo)
except psutil.NoSuchProcess:
pass