Cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python
npx @tessl/cli install tessl/pypi-psutil@7.0.0Cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python.
Name: psutil
Language: Python
Installation: pip install psutil
Platforms: Linux, Windows, macOS, FreeBSD, OpenBSD, NetBSD, AIX, Solaris
import psutil
from psutil import Process, Popen{ .api }
import psutil
# System info
print(f"CPU count: {psutil.cpu_count()}")
print(f"Memory: {psutil.virtual_memory()}")
print(f"Boot time: {psutil.boot_time()}")
# Process info
p = psutil.Process() # Current process
print(f"PID: {p.pid}")
print(f"Name: {p.name()}")
print(f"CPU percent: {p.cpu_percent()}")
# List all processes
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
print(proc.info){ .api }
psutil provides two main API categories:
The Process class represents individual system processes and provides comprehensive process management and monitoring capabilities. Processes can be accessed by PID or iterated over.
Module-level functions provide system-wide information about CPU, memory, disk, network, and sensors.
See: System Information Documentation
See: Constants | Exceptions
# Iterate over all processes with specific attributes
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
try:
print(f"{proc.info['pid']}: {proc.info['name']} ({proc.info['cpu_percent']}%)")
except (psutil.NoSuchProcess, psutil.AccessDenied):
pass{ .api }
# Get system-wide CPU and memory usage
cpu_percent = psutil.cpu_percent(interval=1)
cpu_times_percent = psutil.cpu_times_percent(interval=1) # Detailed CPU time breakdown
memory = psutil.virtual_memory()
print(f"CPU: {cpu_percent}%, Memory: {memory.percent}%")
print(f"CPU User: {cpu_times_percent.user}%, System: {cpu_times_percent.system}%"){ .api }
# Find and terminate processes by name
for proc in psutil.process_iter(['pid', 'name']):
if 'python' in proc.info['name']:
proc.terminate() # or proc.kill() for force{ .api }
try:
p = psutil.Process(1234)
print(p.name())
except psutil.NoSuchProcess:
print("Process not found")
except psutil.AccessDenied:
print("Permission denied")
except psutil.ZombieProcess:
print("Process is zombie"){ .api }
# Quick system overview (similar to ps/top command)
psutil.test() # Prints comprehensive system information to stdout
# Version information
print(f"psutil version: {psutil.__version__}")
# Platform-specific features
if psutil.WINDOWS:
# Windows service management
for service in psutil.win_service_iter():
if service.status() == 'running':
print(f"Running service: {service.name()} - {service.display_name()}")
elif psutil.LINUX:
# Linux-specific resource limits
p = psutil.Process()
if hasattr(psutil, 'RLIMIT_NOFILE'):
soft, hard = p.rlimit(psutil.RLIMIT_NOFILE)
print(f"File descriptor limits: soft={soft}, hard={hard}"){ .api }
psutil provides cross-platform APIs with consistent interfaces. Some features may have platform-specific availability or behavior differences. Platform-specific features are clearly documented in the respective sections.
Supported platforms: Linux, Windows, macOS, FreeBSD, OpenBSD, NetBSD, AIX, Solaris