面试题答案
一键面试#!/bin/bash
cpu_usage=$(top -bn1 | grep '%Cpu(s)' | awk '{print $2 + $4}')
mem_usage=$(free -h | awk '/Mem:/{print $3/$2 * 100}' | cut -d '.' -f 1)
if test $cpu_usage -gt 80 -a $mem_usage -gt 90
then
echo "CPU使用率: $cpu_usage%,内存使用率: $mem_usage%" | mail -s "系统资源警告" 管理员邮箱
echo "$(date) CPU使用率: $cpu_usage%,内存使用率: $mem_usage%" >> /var/log/resource_warning.log
else
echo "系统资源使用正常"
fi