shell script for cpu utilization

Shell script to get CPU and memory utilization

As a system administrator, you will need to frequently keep track of CPU and memory utilization. So it is advisable to use a shell script for this purpose, to automatically calculate these metrics and keep you updated. In this article, we will look at how to create shell script to get CPU and memory utilization in Linux.


Shell script to get CPU and memory utilization

Here are the steps to create shell script to get CPU and memory utilization.


1. Create empty shell script

Open terminal and run the following command to create a blank shell script.

$ sudo vi system_stats.sh


2. Calculate CPU and memory usage

Add the following lines to calculate CPU and memory utilization.

#!/bin/bash
echo `date`
#cpu use threshold
cpu_threshold='80'
 #mem idle threshold
mem_threshold='100'
 #disk use threshold
disk_threshold='90'
#---cpu
cpu_usage () {
cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}'|cut -f 1 -d "."`
cpu_use=`expr 100 - $cpu_idle`
 echo "cpu utilization: $cpu_use"
if [ $cpu_use -gt $cpu_threshold ]
    then
        echo "cpu warning!!!"
    else
        echo "cpu ok!!!"
fi
}
#---mem
mem_usage () {
 #MB units
mem_free=`free -m | grep "Mem" | awk '{print $4+$6}'`
 echo "memory space remaining : $mem_free MB"
if [ $mem_free -lt $mem_threshold  ]
    then
        echo "mem warning!!!"
    else
        echo "mem ok!!!"
fi
}
#---disk
disk_usage () {
disk_use=`df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"`
 echo "disk usage : $disk_use" 
if [ $disk_use -gt $disk_threshold ]
    then
        echo "disk warning!!!"
    else
        echo "disk ok!!!"
fi
 
 
}
cpu_usage
mem_usage
disk_usage

In the above code, we have defined 3 function cpu_usage, mem_usage and disk_usage to calculate cpu, memory and disk utilization. We have also defined threshold values for cpu, memory and disk.

For calculating CPU usage, we run the top command, and use grep + awk to extract idle CPU % from its output. Then we subtract it from 100 to calculate CPU usage.

We use free command to calculate memory usage and df command to calculate disk usage.

Further, we also check each of these values against their respective threshold values, and display appropriate warnings if they exceed threshold values.


3. Make Shell Script Executable

Run the following command to make shell script executable.

$ sudo chmod +x system_stats.sh


4. Test shell script

You can run your shell script using the following command

./system_stats.sh
cpu usage : 35%
memory space remaining : 3330 MB
disk usage : 21%


5. Create Cron Job

You will need to frequently inspect cpu, memory and disk usage so it is advisable to create a cronjob for it. Open crontab with the following command.

$ sudo crontab -e

Add the following lines to it.

0 10 * * * ./system_stats.sh >>/opt/system.log

In the above code, we setup a cronjob that runs system_stats.sh shell script every day at 10.a.m and appends the output to /opt/system.log. You can change it as per your requirement.

This way you will have a daily record of your system information in a single file. You can simply view it with the following command.

$ sudo cat /opt/system.log

In this article, we have learnt how to create a simple shell script to track the 3 most important metrics of any Linux system – CPU usage, memory usage, disk usage. You can use it to keep track of system information and resource utilization regularly.

Also read:

How to Check SSD Health in Linux
How to Set Default Gateway in Linux
How to Touch All Files in Directory
How to Exit For Loop in Shell Script
How to Set Default Python Version

Leave a Reply

Your email address will not be published. Required fields are marked *