Bash script to monitor CPU and Memory usage on Linux

Memory usage on Linux is generally measured in terms of the amount of RAM being used by a particular process. This can be monitored by using the free command which displays the total amount of memory, used memory, and free memory available on the system.

CPU usage on Linux is generally measured in terms of the number of CPU cores being used by a particular process. This can be monitored by using the top command which displays the amount of CPU cores and the total amount of CPU time being used.

It is possible to monitor CPU and memory usage on Linux with a Bash script. This can be accomplished using the command line tools top, free, and ps. The top command displays the resource usage of all running processes on the system, while the free command provides more detailed information about the system’s memory usage. The ps command can be used to view information about a specific process.
In this tutorial you will learn:

  • How to monitor CPU and Memory usage with bash script
simple bash script to monitor memory and cpu
Simple Bash script to monitor memory and cpu on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software gdisk, gparted
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to format a disk on Linux via command line



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!

DID YOU KNOW?
Zombie processes do not use CPU in Linux. A zombie process is a process that has completed execution but still has an entry in the process table. The process is basically “dead” but has not yet been removed from the process table

The Bash script can be used to check the system performance in real time, as well as to collect usage statistics over time. The script can collect usage information from the /proc/stat file and then store it in a log file. The Bash script can also be configured to send an email alert when the CPU or memory usage exceeds a certain threshold.

Additionally, the Bash script can be used to query the system for information such as CPU model, usage, and load averages. With this information, the script can be used to create reports and graphs that provide an overview of the system’s usage.

A simple continuous CPU and memory monitoring script on Linux as shown below is a great way to keep track of the performance of your system:

#!/bin/bash
# This script monitors CPU and memory usage

while :
do 
  # Get the current usage of CPU and memory
  cpuUsage=$(top -bn1 | awk '/Cpu/ { print $2}')
  memUsage=$(free -m | awk '/Mem/{print $3}')

  # Print the usage
  echo "CPU Usage: $cpuUsage%"
  echo "Memory Usage: $memUsage MB"
 
  # Sleep for 1 second
  sleep 1
done

Save the above script into text file, make it executable and run. The following is an terminal output of the above CPU and Memory monitoring script.




Bash script to monitor CPU and Memory usage on Linux
Bash script to monitor CPU and Memory usage on Linux

The following are other tools/commands which can be used to monitor CPU and Memory usage on a Linux system:

  • free: This command shows the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel.
  • top: This command provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.
  • htop: This is an interactive process viewer for Linux. It is a text-mode application that displays process information in a user-friendly way.
  • vmstat: This command reports information about processes, memory, paging, block IO, traps, and CPU activity.
  • ps: This command is used to display information about the currently running processes.
  • iostat: This command reports Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions.

Conclusion

The simple continuous CPU and Memory monitoring script on Linux is an effective and reliable tool for monitoring the system performance. It allows system administrators to efficiently and quickly monitor the system performance in real-time. The script is easy to configure and requires minimal maintenance. Its ability to generate reports and alert system administrators makes it an invaluable resource for a system administrator.



Comments and Discussions
Linux Forum