Operations 18 min read

How to Monitor Linux CPU, Memory, and Disk I/O with a Bash Script

This article provides a step‑by‑step Bash script that gathers Linux server IP, CPU core count, utilization percentages, context switches, load averages, memory statistics, swap usage, and disk I/O metrics, explaining each command and its output for effective system monitoring.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Monitor Linux CPU, Memory, and Disk I/O with a Bash Script

Script Overview

The script collects Linux system information such as IP address, CPU core count, CPU utilization, context switches, load averages, memory usage, swap usage, and disk I/O statistics, then prints each value.

#!/bin/bash
# Get local server IP address
IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`
echo "IP地址:"$IP

# Get total CPU cores
cpu_num=`grep -c "model name" /proc/cpuinfo`
echo "cpu总核数:"$cpu_num

# CPU utilization
cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
echo "用户空间占用CPU百分比:"$cpu_user
cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
echo "内核空间占用CPU百分比:"$cpu_system
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
echo "空闲CPU百分比:"$cpu_idle
cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
echo "等待输入输出占CPU百分比:"$cpu_iowait

# CPU interrupts and context switches
cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
echo "CPU中断次数:"$cpu_interrupt
cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
echo "CPU上下文切换次数:"$cpu_context_switch
cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`
echo "CPU任务队列长度:"$cpu_task_length

# Load averages
cpu_load_15min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
echo "CPU 15分钟前到现在的负载平均值:"$cpu_load_15min
cpu_load_5min=`uptime | awk '{print $10}' | cut -f 1 -d ','`
echo "CPU 5分钟前到现在的负载平均值:"$cpu_load_5min
cpu_load_1min=`uptime | awk '{print $9}' | cut -f 1 -d ','`
echo "CPU 1分钟前到现在的负载平均值:"$cpu_load_1min

# Memory information
mem_total=`free | grep Mem | awk '{print $2}'`
echo "物理内存总量:"$mem_total
mem_sys_used=`free | grep Mem | awk '{print $3}'`
echo "已使用内存总量(操作系统):"$mem_sys_used
mem_sys_free=`free | grep Mem | awk '{print $4}'`
echo "剩余内存总量(操作系统):"$mem_sys_free
mem_user_used=`free | sed -n 3p | awk '{print $3}'`
echo "已使用内存总量(应用程序):"$mem_user_used
mem_user_free=`free | sed -n 3p | awk '{print $4}'`
echo "剩余内存总量(应用程序):"$mem_user_free
mem_swap_total=`free | grep Swap | awk '{print $2}'`
echo "交换分区总大小:"$mem_swap_total
mem_swap_used=`free | grep Swap | awk '{print $3}'`
echo "已使用交换分区大小:"$mem_swap_used
mem_swap_free=`free | grep Swap | awk '{print $4}'`
echo "剩余交换分区大小:"$mem_swap_free

# Disk I/O statistics (device /dev/sda)
echo "指定设备(/dev/sda)的统计信息"
disk_sda_rs=`iostat -kx | grep sda | awk '{print $4}'`
echo "每秒向设备发起的读请求次数:"$disk_sda_rs
disk_sda_ws=`iostat -kx | grep sda | awk '{print $5}'`
echo "每秒向设备发起的写请求次数:"$disk_sda_ws
disk_sda_avgqu_sz=`iostat -kx | grep sda | awk '{print $9}'`
echo "向设备发起的I/O请求队列长度平均值"$disk_sda_avgqu_sz
disk_sda_await=`iostat -kx | grep sda | awk '{print $10}'`
echo "每次向设备发起的I/O请求平均时间:"$disk_sda_await
disk_sda_svctm=`iostat -kx | grep sda | awk '{print $11}'`
echo "向设备发起的I/O服务时间均值:"$disk_sda_svctm
disk_sda_util=`iostat -kx | grep sda | awk '{print $12}'`
echo "向设备发起I/O请求的CPU时间百分占比:"$disk_sda_util

Execution Result

Script execution output
Script execution output

Principle Explanation

Understanding the commands used in the script helps you adapt it to different environments.

1. Retrieve Local Server IP

IP=`ifconfig | grep inet | grep -vE 'inet6|127.0.0.1' | awk '{print $2}'`
echo "IP地址:"$IP

ifconfig | grep inet filters lines containing inet .

grep -vE 'inet6|127.0.0.1' removes IPv6 and loopback entries.

awk '{print $2}' extracts the second column, which holds the IP address.

The result is assigned to IP and printed.

2. Get Total CPU Cores

cpu_num=`grep -c "model name" /proc/cpuinfo`
echo "cpu总核数:"$cpu_num
/proc/cpuinfo

lists CPU details; each occurrence of model name represents a core. grep -c counts those occurrences, yielding the core count.

3. CPU Utilization

The top command provides real‑time CPU usage. The script extracts user, system, idle, and iowait percentages using grep, awk, and cut.

cpu_user=`top -b -n 1 | grep Cpu | awk '{print $2}' | cut -f 1 -d "%"`
echo "用户空间占用CPU百分比:"$cpu_user
cpu_system=`top -b -n 1 | grep Cpu | awk '{print $4}' | cut -f 1 -d "%"`
echo "内核空间占用CPU百分比:"$cpu_system
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}' | cut -f 1 -d "%"`
echo "空闲CPU百分比:"$cpu_idle
cpu_iowait=`top -b -n 1 | grep Cpu | awk '{print $10}' | cut -f 1 -d "%"`
echo "等待输入输出占CPU百分比:"$cpu_iowait

4. CPU Interrupts and Context Switches

cpu_interrupt=`vmstat -n 1 1 | sed -n 3p | awk '{print $11}'`
echo "CPU中断次数:"$cpu_interrupt
cpu_context_switch=`vmstat -n 1 1 | sed -n 3p | awk '{print $12}'`
echo "CPU上下文切换次数:"$cpu_context_switch
cpu_task_length=`vmstat -n 1 1 | sed -n 3p | awk '{print $1}'`
echo "CPU任务队列长度:"$cpu_task_length
vmstat

reports virtual memory and CPU activity; -n suppresses repeated headers, sed -n 3p selects the data line, and awk extracts the desired fields.

5. CPU Load Averages

cpu_load_15min=`uptime | awk '{print $11}' | cut -f 1 -d ','`
echo "CPU 15分钟前到现在的负载平均值:"$cpu_load_15min
cpu_load_5min=`uptime | awk '{print $10}' | cut -f 1 -d ','`
echo "CPU 5分钟前到现在的负载平均值:"$cpu_load_5min
cpu_load_1min=`uptime | awk '{print $9}' | cut -f 1 -d ','`
echo "CPU 1分钟前到现在的负载平均值:"$cpu_load_1min
uptime

shows system run time and load averages for the past 1, 5, and 15 minutes. The script isolates each value with awk and removes the trailing comma.

6. Memory Information

mem_total=`free | grep Mem | awk '{print $2}'`
echo "物理内存总量:"$mem_total
mem_sys_used=`free | grep Mem | awk '{print $3}'`
echo "已使用内存总量(操作系统):"$mem_sys_used
mem_sys_free=`free | grep Mem | awk '{print $4}'`
echo "剩余内存总量(操作系统):"$mem_sys_free
mem_user_used=`free | sed -n 3p | awk '{print $3}'`
echo "已使用内存总量(应用程序):"$mem_user_used
mem_user_free=`free | sed -n 3p | awk '{print $4}'`
echo "剩余内存总量(应用程序):"$mem_user_free
mem_swap_total=`free | grep Swap | awk '{print $2}'`
echo "交换分区总大小:"$mem_swap_total
mem_swap_used=`free | grep Swap | awk '{print $3}'`
echo "已使用交换分区大小:"$mem_swap_used
mem_swap_free=`free | grep Swap | awk '{print $4}'`
echo "剩余交换分区大小:"$mem_swap_free
free

reports physical and swap memory. grep Mem selects the memory line, grep Swap selects the swap line, and sed -n 3p obtains the line that shows memory used by applications.

7. Disk I/O Statistics

echo "指定设备(/dev/sda)的统计信息"
disk_sda_rs=`iostat -kx | grep sda | awk '{print $4}'`
echo "每秒向设备发起的读请求次数:"$disk_sda_rs
disk_sda_ws=`iostat -kx | grep sda | awk '{print $5}'`
echo "每秒向设备发起的写请求次数:"$disk_sda_ws
disk_sda_avgqu_sz=`iostat -kx | grep sda | awk '{print $9}'`
echo "向设备发起的I/O请求队列长度平均值"$disk_sda_avgqu_sz
disk_sda_await=`iostat -kx | grep sda | awk '{print $10}'`
echo "每次向设备发起的I/O请求平均时间:"$disk_sda_await
disk_sda_svctm=`iostat -kx | grep sda | awk '{print $11}'`
echo "向设备发起的I/O服务时间均值:"$disk_sda_svctm
disk_sda_util=`iostat -kx | grep sda | awk '{print $12}'`
echo "向设备发起I/O请求的CPU时间百分占比:"$disk_sda_util
iostat -kx

reports extended I/O statistics in kilobytes per second. The script filters for the sda device and extracts read/write request rates, average queue size, await time, service time, and CPU utilization.

Installing Required Tools

The iostat command is part of the sysstat package. Install it on Debian/Ubuntu systems with:

sudo apt-get install sysstat

Reference: https://www.toutiao.com/i6754887380399849998/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MemoryBashdisk
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.