以下是一个简单的 shell 脚本示例,用于监控 cpu 使用率并根据设定的阈值执行操作:
#!/bin/bash# 设置阈值THRESHOLD=80# 无限循环,每隔一段时间检查一次 CPU 使用率while true; do # 使用 mpstat 获取 CPU 使用率 cpu_usage=$(mpstat 1 1 | awk '/Average:/ {print $12}' | cut -d'.' -f1) # 检查 CPU 使用率是否超过阈值 if [ $cpu_usage -gt $THRESHOLD ]; then echo "CPU usage is above the threshold: $cpu_usage%" # 在此处执行你的操作,例如发送通知、重启服务等 else echo "CPU usage is normal: $cpu_usage%" fi # 等待一段时间(例如 60 秒)再次检查 sleep 60done登录后复制
文章来自互联网,不代表电脑知识网立场。发布者:,转载请注明出处:https://www.pcxun.com/n/742294.html
