Files
2024-05-01 12:28:44 -06:00

3.2 KiB

In the domain of Python and Linux, especially when focusing on system-level programming and process management, psutil (Python System and Process Utilities) is an incredibly useful library. It provides an interface for retrieving information on system utilization (CPU, memory, disks, network, sensors) and managing processes. It's a cross-platform library designed to work on Windows, Linux, macOS, FreeBSD, OpenBSD, NetBSD, and Sun Solaris, making it highly versatile for system monitoring and management scripts.

psutil Reference Guide

Installation

pip install psutil

Basic Usage

System Information

psutil allows you to monitor various system metrics easily.

CPU
import psutil

# Get the number of logical CPUs in the system
print("Logical CPUs:", psutil.cpu_count())

# Get the CPU utilization percentage
print("CPU Usage (%):", psutil.cpu_percent(interval=1))
Memory
# Get memory usage details
memory = psutil.virtual_memory()
print("Total Memory:", memory.total)
print("Available Memory:", memory.available)
Disks
# List all mounted disk partitions
partitions = psutil.disk_partitions()
for p in partitions:
    print("Device:", p.device)

# Get disk usage stats
usage = psutil.disk_usage('/')
print("Disk Usage:", usage.percent, "%")
Network
# Get network I/O statistics
net_io = psutil.net_io_counters()
print("Bytes Sent:", net_io.bytes_sent)
print("Bytes Received:", net_io.bytes_recv)
Managing Processes

psutil is also powerful for managing and inspecting processes.

List All Processes
# Iterate over all running processes
for proc in psutil.process_iter(['pid', 'name']):
    print(proc.info)
Kill a Process
# Terminate a process by PID
pid = 12345  # Example PID
process = psutil.Process(pid)
process.terminate()  # Or process.kill()
Process Information
# Get detailed info about a specific process
proc = psutil.Process(pid)
print("Process Name:", proc.name())
print("Process Status:", proc.status())

Cross-platform Compatibility

One of the significant advantages of psutil is its cross-platform nature, allowing scripts written on one OS to be easily ported to another with minimal changes, making it ideal for developing system monitoring tools and utilities that need to run across different environments.

Advanced Features

  • Sensors: Access hardware temperature sensors (e.g., CPU temp).
  • Battery: Check battery status and percentage.
  • Users: List users currently logged in.

Security Considerations

When using psutil for managing processes, especially killing or altering system processes, ensure your scripts are executed with appropriate permissions to avoid unintended system disruptions. Also, consider the security implications of deploying scripts that monitor system and user activity.

psutil bridges the gap between Python scripting and system management tasks, offering a rich set of functionalities for interacting with and monitoring system resources. It's a go-to library for system administrators, developers working on system monitoring tools, or anyone interested in programmatic access to system and process information within Python scripts.