未分类
shell脚本kill掉占用cpu超过90%以上的程序
一个shell脚本,常驻登录节点,监控cpu占用率,如果某一进程占用cpu超过90%,且运行时间超过一分钟,就直接kill掉。shell脚本代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/bin/sh while true do sleep 60 #循环查看占用cpu超过90%的进程ID /bin/ps axf -o "pid %cpu" | awk '{if($2>=90) print $1}' | while read procid do #进程详细信息 pro=$(ps -A|grep "\\<$procid\\>" |sort -k3,3|head -n1) #获取进程运行的时间,如果大于45秒,设time为1,如若不是则设time为0 time="$(echo $pro|awk '{ split($3,tab,/:/); if (tab[3]>=45) {print 1}else{print 0} }')" #如果time1,则kill掉该进程 if [ $time = '1' ];then kill -9 $procid fi done done |
最后后台启动进程: nohup ./kill_high_cpu_proc.sh > cpucheck.log 2>&1 &