CREATE A SCRIPT TO MONITOR THE RAM

Shubham Soni
3 min readNov 21, 2023

--

Photo by Samsung Memory on Unsplash

First of all, create a script file named ram_monitoring.sh

sh means it is a script file and to write our script we are using the vim editor.

$vim ram_monitoring.sh

and press i to work in insert mode in the editor.

Write the shebang line means we have to tell which shell we are currently using to run our script and provide a complete path.

#!/bin/bash

After the shebang line, we have to write a script to monitor the RAM available in our system.

As we know we can use the ‘free’ command to see the RAM status of our system (physical memory & swap memory), we can use options with the free command like -h, -m, -t,…

So here we use “free -mt” which means we are calling the information regarding RAM with the options -m and -t.

-m is for showing the memory specs in MiB

-t is for showing the total, used and free memory of the system.

free -mt

After that, we only require the total aspects of memory from the output, so we can use the ‘grep’ command to filter out the desired output.

free -mt | grep “Total”

the above command means to run free -mt and when it completely executes then fire grep “Total” query on the output and it will give the final output as total specs of the memory.

After this, we get the output as shown below:

As we only require the leftover memory of the system, not the total memory or used memory, we can filter out the output to the desired output using the “awk” command

free -mt | grep “Total” | awk ‘{print $4}’

awk is used for pattern processing in the Linux O.S. Here we use awk to process the output and filter out the output that is on number 4.

The output is as follows:

4748

Now we can store this output in a variable called “FREE_SPACE” as shown below:

FREE_SPACE=$(free -mt | grep "Total" | awk '{print $4}')

Now we can fix a threshold value so that if the total free memory is less than the threshold value it will create a warning.

Create a variable called TH and set a value to it as shown below:

TH=1024

Here we set the threshold value to 1024 mb (set according to your system).

Now to create a warning we will use the if-else condition here as shown below:

if [[ $FREE_SPACE -lt $TH]]
then
echo "WARNING!!!! RAM is running low"
else
echo "RAM space is sufficient- $FREE-SPACE MiB "
fi

Here we compare the free space of the system and threshold value so that if the value of FREE_SPACE becomes less than TH then it will echo a warning to the terminal, if the FREE_SPACE is equal to or more than the TH value then it will echo RAM space is sufficient and will print the current available status of the RAM. Then we will end the if-else condition by writing “fi” at the end of the condition.

Here, the scripting is completed and to save the file and exit the editor press ESC and the write

:wq

As we have created the script above, now we have to execute or run the script.

There are the following ways to execute a script, which are as below:

1. use chmod +x ram_monitoring.sh to provide the execute permission of the file to all and then execute the script using ram_monitoring.sh

2. Direct execute using bash ram_monitoring.sh

Also, you can visit my Github Repository for the complete script:

THANK YOU

--

--

No responses yet