---
title: "Create server load monitoring bash script"
description: "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..."
url: https://www.inmotionhosting.com/support/server/server-usage/create-server-load-monitoring-bash-script/
date: 2012-12-04
modified: 2021-08-16
author: "InMotion Hosting Contributor"
categories: ["Server Usage"]
type: post
lang: en
---

# 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](/support/server/ssh/how-to-login-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 ]" recipient@example.com 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 **recipient@example.com** 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 ]” recipient@example.com 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](/support/). Â  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.
