shell脚本如下:
#!/bin/bash #CPU cpu_idle=$(top -n1 | grep 'Cpu' | tail -n 1 | awk '{print $5}') cpu_idle_num=`echo $cpu_idle | awk -F"%" '{print $1}'` cpu_usage=$(echo "100 - $cpu_idle_num" | bc) #Mem mem_free=$(free -m | awk '/Mem:/{print $4 + $6 + $7'}) mem_total=$(free -m | awk '/Mem:/{print $2}') mem_used=$(echo "$mem_total - $mem_free" | bc) mem_rate=$(echo "$mem_used * 100 / $mem_total" | bc) #disk disk_usage=$(df -hP / | tail -n 1 | awk '{print $5}') disk_used=$(df -hP / | tail -n 1 | awk '{print $3}') echo "cpu利用率: $cpu_usage %" echo "内存使用量: $mem_used M" echo "内存使用率: $mem_rate %" echo "磁盘空间使用量: $disk_used" echo "磁盘空间利用率: $disk_usage"
执行效果: