vi checkcpu.sh
linux下cpu超100%时kill掉pid
#!/bin/bash
# 定义一个函数来检查CPU使用率并杀死进程
check_kill_process() {
top_output=$(top -b -n 1 | grep "R" | awk '{if($9 > 1000.0) print $1}')
for pid in $top_output; do
kill -9 $pid 2>/dev/null
echo "Killed PID $pid because its CPU usage is greater than 100%"
done
}
# 主循环,每隔一定时间运行检查函数
while true; do
check_kill_process
sleep 5 # 每隔5秒检查一次
done
chmod +x checkcpu.sh
redis出现停用时自动重启脚本
redis.sh
#! /bin/bash
#by yuanzelin8
#2021-01-08
#6379是redis监听的端口;
port=`netstat -an | grep 6379 | wc -l`
if [ $port -lt 2 ]; then
systemctl restart redis
fi
chmod +x redis.sh