psutil

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by UNIX command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap. psutil currently supports the following platforms:

Example usages

CPU

import psutil
psutil.cpu_times()

scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)

 for x in range(3):
     psutil.cpu_percent(interval=1)

4.0 5.9 3.8

 for x in range(3):
     psutil.cpu_percent(interval=1, percpu=True)

[4.0, 6.9, 3.7, 9.2] [7.0, 8.5, 2.4, 2.1] [1.2, 9.0, 9.9, 7.2]

 for x in range(3):
     psutil.cpu_times_percent(interval=1, percpu=False)

scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0) scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

 psutil.cpu_count()

4

2

scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)

scpufreq(current=931.42925, min=800.0, max=3500.0)

Memory

sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)

Disks

[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'), sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw')]

sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)

sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)

Network

{'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0), 'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}

Sensors

Other system info

psutil.boot_time() 1365519115.0

Process management

'python'

'/usr/bin/python'

'/home/giampaolo'

['/usr/bin/python', 'main.py']

7055

7054

'running'

'giampaolo'

1267551141.5019531

'/dev/pts/0'

puids(real=1000, effective=1000, saved=1000)

pgids(real=1000, effective=1000, saved=1000)

pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1)

12.1

[0, 1, 2, 3]

1

pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)

pfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)

0.7823

pio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)

[popenfile(path='/home/giampaolo/svn/psutil/setup.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monitd', fd=4, position=235542, mode='a', flags=33793)]

[pconn(fd=115, family=, type=, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED'), pconn(fd=117, family=, type=, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING'), pconn(fd=119, family=, type=, laddr=addr(ip='10.0.0.1', port=60759), raddr=addr(ip='72.14.234.104', port=80), status='ESTABLISHED'), pconn(fd=123, family=, type=, laddr=addr(ip='10.0.0.1', port=51314), raddr=addr(ip='72.14.234.83', port=443), status='SYN_SENT')]

4

8

[pthread(id=5234, user_time=22.5, system_time=9.2891), pthread(id=5235, user_time=0.0, system_time=0.0), pthread(id=5236, user_time=0.0, system_time=0.0), pthread(id=5237, user_time=0.0707, system_time=1.1)]

pctxsw(voluntary=78, involuntary=19)

0

(5, 5)

True

0

Further process APIs

{'pid': 1, 'name': 'systemd'} {'pid': 2, 'name': 'kthreadd'} {'pid': 3, 'name': 'ksoftirqd/0'} ...

True

waits for multiple processes to terminate

gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)

Popen wrapper:

'python'

'giampaolo'

('hello\n', None)

0

Windows services

Other samples See doc recipes.

Last updated

Was this helpful?