Files
sh/check_cpu.sh
2024-01-12 13:58:38 +08:00

22 lines
583 B
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 定义颜色
green="\e[1;32m"
yellow="\e[1;33m"
re="\033[0m"
# 设置默认CPU阈值为99单位%
threshold_cpu=99
# 获取当前 CPU 使用率
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | awk -F. '{print $1}')
echo -e "${green}当前CPU使用率: ${cpu_usage}%${re}"
# 检查 CPU 使用率是否超过阈值
if ((cpu_usage >= threshold_cpu)); then
echo -e "${yellow}CPU使用率已达到${threshold_cpu}%, 执行关机操作...${re}"
shutdown -h now
else
echo -e "${green}CPU使用率未达到${threshold_cpu}%, 继续运行...${re}"
fi