Create server load monitoring bash script

In this article we’ll discuss how you can setup a small server load monitoring bash script with 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:

    #!/bin/bash trigger=4.00 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" ]] then sar -q | mail -s"High load on server - [ $load ]" [email protected] fi

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

    #!/bin/bash

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

    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 [email protected]
    this sends an e-mail with the server’s recent load averages there.
    if [[ $response = “greater” ]] then
    sar -q | mail -s”High load on server – [ $load ]” [email protected]
    fi

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.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

15 thoughts on “Create server load monitoring bash script

  1. Also,
    you might want to mention that
    “`
    */5 * * * * bash /home/userna1/loadMon
    “`
    has to be placed in crontab
    ie.

    “`
    crontab -e
    “`
    hit ‘i’

    then the command you want to cron

    then save and exit

    “` :wq “`


    took a while to figure out
    so here’s the tip 😛

  2. Hi, you might not do custom bash, but I am having the same error on centos6.  So perhaps your code is not written for every version of linux.

    # ./cpucheck.sh

    ./cpucheck.sh: line 13: syntax error near unexpected token `(‘

     

    ./cpucheck.sh: line 13: `response=`echo | awk -v T=$trigger -v L=$load ‘BEGIN{if ( L > T){ print “greater”}}’`’

     

    1. We are happy to help, but it is not clear what you are trying to accomplish. Could you provide additional details or information?

      Thank you,
      John-Paul

  3. I get an error 

    /myMonitor/loadMonitor: line 13: syntax error near unexpected token `(‘

     

    /myMonitor/loadMonitor: line 13: `response=`echo | awk -v T=$trigger -v L=$load ‘BEGIN{if ( L > T){ print “greater”}}’`’

    1. Hello,

      This error states the file you are trying to load does not exist or you do not have permissions to view it.

      Best Regards,
      TJ Edens

    2. Hello,

      Unfortunately we do not assist with custom code solutions. I would suggest some tutorials on Bash programming or get with someone locally that knows the scripting language.

      Best Regards,
      TJ Edens

    3. Hello,

      Also I just used the code above and had no issues running it as the root user on a VPS.

      Best Regards,
      TJ Edens

  4. Hi there, i make the bash file but I received a mail” 

    “bash: /home/root/myMonitor/loadMonitor: No such file or directory”

     

     

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+