| Sunday May 29th 2016

HOWTO: Create a shell script to monitor load average on a Linux server

Linux Tux logoLoad Average on a server reflects the current state of the server. Higher the load average, poorer is the server performance hence it is a necessity to monitor the load average on the server. The following shell script monitors the load average on the Linux server and inform the server administrator with the current running processes of the server if the load average is greater than the defined threshold.

Create a file such as /root/monitor-load-average.sh and paste the following script in it:


# Define Variables
CUR_TIME=`date +"%A %b %e %r"`

# Retrieve the load average of the past 1 minute
Load_AVG=`uptime | cut -d’l’ -f2 | awk ‘{print $3}’ | cut -d. -f1`
LOAD_CUR=`uptime | cut -d’l’ -f2 | awk ‘{print $3 " " $4 " " $5}’ | sed ’s/,//’`

# Define Threshold. This value will be compared with the current load average. Set the value as per your wish.

# Compare the current load average with Threshold and email the server administrator if threshold is greater.

if [ $Load_AVG -gt $LIMIT ]

#Save the current running processes in a file
/bin/ps -auxf >> /root/ps_output

echo "Current Time :: $CUR_TIME" >> /tmp/monitload.txt
echo "Current Load Average :: $LOAD_CUR" >> /tmp/monitload.txt
echo "The list of current processes is attached with the email for your reference." >> /tmp/monitload.txt
echo "Please Check… ASAP."  >> /tmp/monitload.txt

# Send an email to the administrator of the server
/usr/bin/mutt -s "ALERT!!! High 1 minute load average on ‘$HOSTNAME’" -a /root/ps_output youremail@yourdomain.tld < /tmp/monitload.txt


# Remove the temporary log file
/bin/rm -f /tmp/monitload.txt
/bin/rm -f /root/ps_output

Now, schedule a cronjob to execute the script on per minute basis. Edit the cronjob file

# crontab -e

and place the following cronjob at the end of the file

* * * * * /bin/sh /root/monitor-load-average.sh

In order to use mutt to send emails, you need to install the mutt package on the server. It allows you to send emails with attachments. To install on a Redhat/Fedora/CentOS server type:

# yum install mutt

Related Posts: On this day...

Reader Feedback

One Response to “HOWTO: Create a shell script to monitor load average on a Linux server”

  1. Myliteraturetechlife.com says:

    this script return error
    Can’t stat email@domain.com: No such file or directory
    email@domain.com: unable to attach file.

    I replaced my email address with “email@domain.com” in above error..

Leave a Reply

You must be logged in to post a comment.