How to check CPU/RAM/Disk usage in Ubuntu

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Monitoring system resources is an essential task for any system administrator or regular user looking to manage their Ubuntu machine’s performance. Keeping track of the CPU, RAM, and Disk usage can provide insights into how applications are running and what improvements can be made for optimization. This tutorial aims to introduce various commands and tools available in Ubuntu to monitor these vital resources.

System Monitor

The Ubuntu System Monitor is a graphical tool that provides a real-time overview of the system’s current processes along with CPU, RAM, and disk usage. You can easily access it from the Dashboard or by typing ‘System Monitor’ in the Application Launcher. For a command-line equivalent, the ‘top’ command is widely used.

top

Upon entering the ‘top’ command, an interactive window displays the processes using the most CPU and memory at the top of the list.

htop – An Interactive Process Viewer

‘htop’ is an improved version of ‘top’. It provides a better, color-coded display, and the ability to scroll horizontally and vertically, allowing you to see all the processes running on your system and their resource usage. You may first need to install htop if it’s not already on your system:

sudo apt-get install htop
htop

After installation, just type ‘htop’ to open the interactive viewer.

Checking CPU Usage

The ‘mpstat’ command is part of the ‘sysstat’ package and can provide detailed reports of CPU activity.

sudo apt-get install sysstat
mpstat -P ALL 1

This command reports on the activity of each processor in one-second intervals.

Monitoring RAM Usage

The ‘free’ command gives a snapshot of memory usage:

free -m

The ‘-m’ flag displays the output in megabytes. For more detailed memory statistics, you can use the vmstat command:

vmstat -s

It offers an overview of processes, memory, paging, block I/O, traps, and CPU activity.

Analyzing Disk Usage

The ‘df’ command shows disk space usage on all mounted filesystems.

df -h

The ‘-h’ (human-readable) option makes the output more understandable by converting data into bytes, KB, MB, or GB.

To view disk usage of each directory recursively, you can use the ‘du’ command:

du -h --max-depth=1

This command starts from the root directory and reports the disk usage of each subdirectory.

Advanced Techniques

For more advanced system resource monitoring, installing ‘iotop’ or ‘nmon’ could be very beneficial.

sudo apt-get install iotop
iotop

At any point in time, ‘iotop’ will show which process is writing to or reading from your disk.

sudo apt-get install nmon
nmon

Upon running nmon, you can choose what to monitor. Press ‘c’ for CPU, ‘m’ for memory, and ‘d’ for disk. Nmon also offers network information by pressing ‘n’.

Scripting with Monitoring Tools

To collect system resource usage over time, you can script some of the earlier commands:

(while true; do date; top -b -n1 | head -20; sleep 5; done) > top.log

The above script runs ‘top’ in batch mode every 5 seconds and logs the output along with the current date and time into ‘top.log’.

Similarly, you can also create scripts to monitor RAM and Disk usage over time using ‘free’ and ‘df’ commands combined with similar loop and sleep logic.

Conclusion

Ubuntu offers a variety of tools for checking the CPU, RAM, and Disk usage. Starting from simple commands to more comprehensive monitoring tools, you can choose what’s best suited for your needs and how detailed your monitoring should be. The ability to create scripts based on these tools can help monitor and troubleshoot system performance over time. Understanding how your system resources are utilized is the key to making informed decisions for upgrades or optimizations to ensure smoother operations.