#!/bin/bash
SUBJECT="WARNING CPU USAGE HIGH"
TO=darshankkdarshu@gmail.com
MESSAGE=/tmp/messages
echo "#######################" > $MESSAGE
echo "CPU statistics as follows.." >> $MESSAGE
mpstat >> $MESSAGE
echo "#######################" >> $MESSAGE
CPU_USAGE=$(top -b -n1 | awk '/^%Cpu/ {print $2}' | cut -d. -f1)
[ "$CPU_USAGE" -gt 1 ] && mail -s "$SUBJECT" "$TO" < $MESSAGE
The above script sends a mail once CPU usage is more than the limit. Although i have prescribed a vaild gmail id in the code, it is still sending the mail to /var/mail/root
. How to fix this?
I have done the necessary change following the link below. But the /var/mail/root
is showing
Return-Path: <root@server2-SandyBridge-Platform>
Received: by server2-SandyBridge-Platform (Postfix, from userid 0)
id A4977138269C; Fri, 14 Oct 2016 10:53:11 +0530 (IST)
Subject: WARNING CPU USAGE HIGH
To: <darshankkdarshu@gmail.com>
X-Mailer: mail (GNU Mailutils 2.99.98)
Message-Id: <20161014052311.A4977138269C@server2-SandyBridge-Platform>
Date: Fri, 14 Oct 2016 10:53:11 +0530 (IST)
From: root@server2-SandyBridge-Platform (root)
#######################
CPU statistics as follows..
Linux 3.13.0-98-generic (server2-SandyBridge-Platform) Friday 14 October 2016 _x86_64_ (4 CPU)
10:53:11 IST CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
10:53:11 IST all 3.07 0.15 1.13 1.48 0.00 0.01 0.00 0.00 0.00 94.17
#######################`
There is no mail in my mail id though.@Terrance
top -b -n1 | awk '/^%Cpu/ {print $2}'
to your question. With my old Ubuntu 11.04 there is not output and with 16.04 output is0.8
. – Cyrus Oct 14 '16 at 05:42/var/mail/root
shows like this – darshan krishnaiah Oct 14 '16 at 06:04