python获取linux系统负载 To check the system load on a Linux machine using Python

Published on Aug. 22, 2023, 12:14 p.m.

To check the system load on a Linux machine using Python, you can use the os module. Specifically, you can use the os.getloadavg() function to get the load averages for the system over the last 1, 5, and 15 minutes.

Here’s an example code snippet that prints the load averages:

import os

load_avgs = os.getloadavg()
print("Load averages over the last 1, 5, and 15 minutes:", load_avgs)

This will output something like:

Load averages over the last 1, 5, and 15 minutes: (1.14, 0.80, 0.42)

The load averages indicate the average number of processes that are either in a runnable or uninterruptable state over the given time period. Typically, you want to keep the load averages below the number of CPU cores available on the system in order to ensure that the system is not overly taxed. However, the actual thresholds for what is considered an acceptable load will depend on your specific use case and system configuration.

Tags: