Monitor CPU usage using Python
Monitoring CPU usage is a crucial aspect of system performance analysis, whether you’re running Windows, Linux, or macOS. In this article, we will explore how to monitor CPU usage using Python scripts on all three of these popular operating systems.
CPU monitoring allows you to track how much of your system’s processing power is being utilized by various processes and applications. This information is valuable for system administrators, developers, and anyone interested in optimizing system performance or diagnosing issues.
We will use Python to create scripts that utilize platform-specific libraries to monitor CPU usage. Here’s a step-by-step guide for each operating system:
Windows
To monitor CPU usage on Windows, we will use the psutil
library. If you haven’t already installed it, you can do so with pip:
pip install psutil
Now, let’s create a Python script to monitor CPU usage:
import psutil
def monitor_cpu_usage():
while True:
cpu_usage = psutil.cpu_percent(interval=1)
print(f"CPU Usage: {cpu_usage}%")
if __name__ == "__main__":
monitor_cpu_usage()
This script imports the psutil
library and repeatedly checks and prints the CPU usage percentage with a 1-second interval. You can stop the script by pressing Ctrl+C.
Linux
On Linux, we can use the psutil
library just like on Windows, but there’s also an alternative method using the /proc/stat
file.
First, ensure you have psutil
installed
Let’s create a Python script that monitors CPU usage on Linux using /proc/stat:
import time
def monitor_cpu_usage():
while True:
with open('/proc/stat') as file:
line = file.readline()
if line.startswith('cpu '):
fields = line.split()
total_time = sum(map(int, fields[1:]))
idle_time = int(fields[4])
cpu_usage = 100.0 * (1.0 - idle_time / total_time)
print(f"CPU Usage: {cpu_usage:.2f}%")
time.sleep(1)
if __name__ == "__main__":
monitor_cpu_usage()
This script reads the /proc/stat
file and calculates CPU usage based on the idle and total time. It prints the CPU usage percentage with a 1-second interval.
macOS
macOS monitoring can be done using the psutil
library, similar to Windows. Ensure you have psutil
installed:
Create a Python script for monitoring CPU usage on macOS:
import psutil
def monitor_cpu_usage():
while True:
cpu_usage = psutil.cpu_percent(interval=1)
print(f"CPU Usage: {cpu_usage}%")
if __name__ == "__main__":
monitor_cpu_usage()
We used the psutil
library for Windows and macOS, which provides a cross-platform solution. For Linux, we used /proc/stat
to calculate CPU usage.
Windows
Sample output from the Windows script using the psutil
library:
CPU Usage: 10.5%
CPU Usage: 8.2%
CPU Usage: 12.7%
Linux
Sample output from the Linux script using the /proc/stat
file:
CPU Usage: 2.12%
CPU Usage: 4.32%
CPU Usage: 1.98%
macOS
Sample output from the macOS script using the psutil
library:
CPU Usage: 8.5%
CPU Usage: 6.7%
CPU Usage: 9.1%
Refer more on python here : Python
Read more on Shell and Linux related articles
Read more on Airflow here : Airflow