You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux系统脚本中仅获取负载平均值的命令求助

Extract Load Average from Uptime (Linux)

Great question! Parsing uptime to get just the load average is a common task for scripts, and there are several reliable ways to do this depending on whether you need all three load values (1-minute, 5-minute, 15-minute) or just a specific one.

Option 1: Get the full load average line (matches your example format)

If you want output like load average: 0.12, 0.10, 0.09, use awk to split the uptime output at the "load average:" string:

uptime | awk -F 'load average:' '{print "load average:" $2}'

This works because we set the field separator to "load average:", then print the label plus the second field which contains all three load values.

Option 2: Get a single specific load average (e.g., 1-minute value)

If you only need the 1-minute load average (like your example load average: 0.11), you can extend the awk command to pick just the first value:

uptime | awk -F 'load average:' '{print "load average:" $2}' | awk '{print $1}'

Or for a faster approach (no need to run uptime), read directly from the /proc/loadavg system file (this is what uptime uses under the hood):

awk '{print "load average:" $1}' /proc/loadavg

The /proc/loadavg file lists the three load averages as its first three fields, so this is a lightweight, distro-agnostic method.

Option 3: Use sed for pattern matching

If you prefer sed, you can use a regex to extract everything after "load average:":

uptime | sed -n 's/.*load average: \(.*\)/load average: \1/p'

The -n flag tells sed to only print lines that match the pattern, and the regex captures the load values after the label.

All these commands are safe for scripting since they don't rely on fixed column positions (which can change if your uptime string has varying lengths, like longer uptimes or more users).

内容的提问来源于stack exchange,提问作者Alexandre Carvalho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:31:36