在某些特殊场景需求下,需要centos在cpu负载过高cpu100%时,能够自动重启某些应用程序
1. 重启脚本
编写检查cpu负载和重启应用的脚本
vim /data/script/check_cpu_load.sh
#!/bin/sh
#crontab: * * * * * /data/script/check_cpu_load.sh >> /data/script/check_cpu_load.log
TOP_SYS_LOAD_NUM=10
SYS_LOAD_NUM=`uptime | awk '{print $(NF-2)}' | sed 's/,//'`
echo $(date +"%y-%m-%d") `uptime`
if [ `echo "$TOP_SYS_LOAD_NUM < $SYS_LOAD_NUM"|bc` -eq 1 ]; then
echo "restarting iosmdm provider"
supervisorctl restart gnw-push-iosmdm-provider
echo "restarted."
fi
给到执行权限
chmod +x /data/script/check_cpu_load.sh
2. 新建crontab定时任务
crontab -e
* * * * * /data/script/check_cpu_load.sh >> /data/script/check_cpu_load.log
3. 查看执行日志
定时任务每分钟执行一次检查
tail -f /data/script/check_cpu_load.log
20-02-20 12:24:01 up 6 days, 16:48, 1 user, load average: 13.29, 9.48, 5.19
20-02-20 12:25:01 up 6 days, 16:49, 1 user, load average: 15.73, 10.86, 5.93
20-02-20 12:26:01 up 6 days, 16:50, 1 user, load average: 18.12, 12.38, 6.76
20-02-20 12:27:01 up 6 days, 16:51, 1 user, load average: 21.75, 14.44, 7.82
restarting iosmdm provider
gnw-push-iosmdm-provider: stopped
gnw-push-iosmdm-provider: started
restarted.
20-02-20 12:28:01 up 6 days, 16:52, 1 user, load average: 9.15, 12.24, 7.48
20-02-20 12:29:01 up 6 days, 16:53, 1 user, load average: 3.36, 10.01, 7.02
20-02-20 12:30:01 up 6 days, 16:54, 1 user, load average: 1.24, 8.19, 6.58
20-02-20 12:31:01 up 6 days, 16:55, 1 user, load average: 0.55, 6.74, 6.18
20-02-20 12:32:01 up 6 days, 16:56, 1 user, load average: 0.20, 5.52, 5.79
20-02-20 12:33:01 up 6 days, 16:57, 1 user, load average: 0.07, 4.51, 5.43
20-02-20 12:34:01 up 6 days, 16:58, 1 user, load average: 0.16, 3.72, 5.10