InMotion Hosting Support Center

In this article we'll discuss how you can setup a small bash script on a scheduled cron job to monitor your server's load average, and e-mail you a notice if it's above the limit that you've set.

A lot of times your server will probably be running at a load much lower than it can actually handle. Although at certain times your server's usage might suddenly spike leading to the server possibly going unstable and it could potentially need a reboot to recover from.

Our system administration team has monitors setup on all of our dedicated servers for extreme load issues. However you might be interested in setting up some monitoring of your own so that you can be alerted via e-mail as soon as your server is spiking so that you can login to investigate that usage spike more closely.

This would primarily be used for a dedicated server. While it could also be used on a VPS the load average of a VPS typically shouldn't exceed a 1.00 and it can temporarily fluctuate up and down as it's a virtualized environment. So you might end up getting too many notices and by the time you get in to investigate it could have dropped back down.

Create bash load monitoring script

  1. First you'll want to figure out how many CPU cores you have on your dedicated server as this determines the optimal load your server can operate at.


    You can login to your server via SSH and then run the following command to find this out:

    grep pro /proc/cpuinfo -c

    You should get back the number of cores on your server such as  4 .

  2. You won't want your load average spiking much over how many CPU cores your server has, in this case I know this particular server isn't doing anything other than serving up one static website, so if the load gets even up to a 4.00 I'll want to investigate it. So I'm going to set my trigger value for load at a 4.00.


    Start editing a new file to create your bash alert script, in this case I'm using the vim text-editor and making a new file called loadMon in my user's home directory with the following command:

    vim /home/userna1/loadMon

    Then you'll want to hit i to enter Insert mode once vim is loaded up and enter in the following code:

    load=`cat /proc/loadavg | awk '{print $1}'`
    response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`
    if [[ $response = "greater" ]]
    sar -q | mail -s"High load on server - [ $load ]"

    Here is the same code again with comments walking through what each line is doing:


    We set a trigger for how high the load can get before we're
    alerted via e-mail from this script.

    We set a load variable to read the current server load from
    /proc/loadavg and only from the first column which is the live load.
    load=`cat /proc/loadavg | awk '{print $1}'`

    We set a response variable to the word "greater" if the current
    load is greater than our trigger that we set.
    response=`echo | awk -v T=$trigger -v L=$load 'BEGIN{if ( L > T){ print "greater"}}'`

    If the response is set to "greater" we run the sar -q command
    and pipe | that data to the mail command for
    this sends an e-mail with the server's recent load averages there.
    if [[ $response = "greater" ]]
    sar -q | mail -s"High load on server - [ $load ]"

Setup cron job to run monitoring script

  1. Now that you have your load monitoring bash script setup, the next thing you'll want to do is create a cron job to run this task, you can read about how to run a cron job.


    Typically setting up a cron job for monitoring of this type would be fine at every 5 minutes, you can select that premade option in cPanel's cron jobs section, it should end up looking like the following once you're done:

    */5 * * * * bash /home/userna1/loadMon

You should now have successfully setup a small bash script to monitor your servers load average, and a cron job to run that monitoring script on a set interval to send you an e-mail if the server's load has exceeded the limit you've set.

Support Center Login

Social Media Login

Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-03-24 7:04 am

Awsome.. helpful

n/a Points
2014-07-15 4:52 am

This is working fine. You can alter according to your requirements.

n/a Points
2014-07-15 4:55 am
Nothing. Good article.
n/a Points
2016-01-17 8:55 am

Thank you for the useful script!

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

4 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail:
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!