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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解自动杀掉占用较多CPU资源的Shell脚本的相关内容吗,在本文为您仔细讲解自动杀掉占用CPU资源的Shell脚本的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Shell脚本,CPU,下面大家一起来学习吧。

复制代码 代码如下:

#!/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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们