欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

自动杀掉占用较多CPU资源的Shell脚本

程序员文章站 2022-07-20 17:41:52
复制代码 代码如下:#!/bin/bash # march-13-2006# cpuuse trigger script by noel## bash code to w...

复制代码 代码如下:

#!/bin/bash

# march-13-2006
# cpuuse trigger script by noel
#
# bash code to watch a running program's cpu usage.
# if it's above a set value, it will auto send an email.
# you will need to set a cron job to run this script every xx minutes
#
# set some needed things:
#
processtowatch="convert" # in my case i need to watch convert
emailaddress="root@host" # this is my main emailaddress
triggervalue=90 # if the cpu use is above 90% send an email. do not use a dot or comma!
tempfilename=tmp-cpu # some name of the temp file for the ps, grep data

ps auxww | grep "$processtowatch" | grep -v grep > /tmp/$tempfilename
export line
(
read line
while [ -n "$line" ]
do
set $line
read line
if [ $(echo "$3" | sed -e 's/\.[0-9]*//g') -gt $triggervalue ]; then
mail -s "cpu message alert for: $processtowatch" $emailaddress <<-end
this is to inform you that the following process: $processtowatch with pid (process id) $2 is now using more than your preset $triggervalue value.

process: $processtowatch is using: $3 of cpu power!
the command used is: $11
end
fi
done
)< /tmp/$tempfilename