Linux监控重启

如下

#!/bin/bash

# 获取当前的 CPU 占用率
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')

# 获取当前的内存占用率
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')

# 检查 CPU 或内存占用率是否大于 90%
if (( $(echo "$cpu_usage > 90" | bc -l) )) || (( $(echo "$mem_usage > 90" | bc -l) )); then
    echo "CPU 或内存占用率大于 90%,系统将重启。"
    echo "当前 CPU 占用率: $cpu_usage%"
    echo "当前内存占用率: $mem_usage%"
    sudo reboot
else
    echo "CPU 和内存占用率正常。"
    echo "当前 CPU 占用率: $cpu_usage%"
    echo "当前内存占用率: $mem_usage%"
fi

 

posted @ 2024-12-26 09:27  法宝  阅读(9)  评论(0编辑  收藏  举报