In [3]:
!pip3 install psutil
In [4]:
import psutil
In [5]:
psutil.virtual_memory()
Out[5]:
In [6]:
psutil.cpu_percent()
Out[6]:
In [17]:
psutil.virtual_memory()
Out[17]:
In [10]:
pids = psutil.pids()
In [14]:
p = psutil.Process(1)
p
Out[14]:
In [16]:
p.username()
Out[16]:
In [22]:
for p in psutil.process_iter():
if p.username() == "dclong":
print(p.memory_info())
break
In [29]:
for p in psutil.process_iter():
if p.username() == "dclong":
print(f"Memory info: {p.memory_info()}")
print(f"Memory pct: {p.memory_percent()}")
print(f"CPU pct: {p.cpu_percent()}")
break
In [32]:
p.status()
Out[32]:
In [33]:
sum(
p.memory_percent()
for p in psutil.process_iter()
if p.username() == "dclong" and p.status() == "running"
)
Out[33]:
In [42]:
sum(
p.cpu_percent()
for p in psutil.process_iter()
if p.username() == "dclong" and p.status() == "running"
)
Out[42]:
In [2]:
for proc in psutil.process_iter():
print(proc)
Print PIDs of Processes Whose Commands Contain "chrome"¶
In [ ]:
#!/usr/bin/env python3
import psutil
for proc in psutil.process_iter():
if proc.username() == "bendu":
try:
cmd = proc.cmdline()
except:
continue
if "chrome" in cmd[0].lower():
print(proc.pid)
In [ ]: