[英]Bash Script that gets a count of running processes and then emails if over a certain threshold
I am trying to create a script to grab the current number of running processes and the if that number is over 1000 then send me an email. 我正在尝试创建一个脚本来获取当前正在运行的进程数,如果该数字超过1000,则向我发送一封电子邮件。 I am trying to do this in a bash script that I will just use a cron job to call it.
我试图在bash脚本中执行此操作,我将使用cron作业来调用它。 The code I am using is below and I am sure I just have something out of place and just need another set of eyes.
我正在使用的代码如下,我确信我只是有一些不合适的东西,只需要另一组眼睛。
PCOUNT=$(cat /proc/loadavg|awk '{print $4}'|awk -F/ '{print $2}')
if [$PCOUNT > 100]; then
mail -s "Process Count" email@example.com
fi
Your context is incorrect on your if
statement. 您的
if
语句中的上下文不正确。 Try (( ))
instead of []
on your if
like shown below: 尝试
(( ))
而不是[]
您if
下面像图所示:
PCOUNT=$(cat /proc/loadavg|awk '{print $4}'|awk -F/ '{print $2}')
if (( $PCOUNT > 100 )); then
mail -s "Process Count" email@example.com
fi
NOTE: I don't have mail
setup on my system so I could not verify the mail
command. 注意:我的系统上没有
mail
设置,因此无法验证mail
命令。
you can cut down awk to 你可以减少awk
awk -F" |/" '{print $5}' /proc/loadavg awk -F“| /”'{print $ 5}'/ proc / loadavg
if condition 如果条件
[ $PCOUNT -ge 1000 [$ PCOUNT -ge 1000
mail, pass the $PCOUNT 邮件,传递$ PCOUNT
mail -s "Process Count: $PCOUNT" mail -s“进程数:$ PCOUNT”
IMHO, if this is for alert why dont you try nagios plugin 恕我直言,如果这是警告为什么不尝试nagios插件
After alot of trial and error I finally found a solution. 经过大量的反复试验,我终于找到了解决方案。 I ended up taking the output of the awk statement and writing it to a file.
我最终获取了awk语句的输出并将其写入文件。 I then cat the file and send the output to mail.
然后我抓住文件并将输出发送到邮件。
PCOUNT=$(awk -F" |/" '{print $5}' /proc/loadavg)
if ((PCOUNT>1000)); then
echo "Number of Running Processes is" $PCOUNT >>/tmp/mail.txt
cat /tmp/mail.txt | mail -s "Number of processes is rising" example@email.com
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.