A Shell Script to Monitor Network, Disk Usage, Uptime, Load Average and RAM

In a Linux environment, it's essential to keep track of system resources such as network usage, disk usage, uptime, load average, and RAM usage. This information helps system administrators detect potential issues and improve system performance. However, monitoring these metrics manually can be time-consuming and tedious. Therefore, in this article, we will discuss how to use a shell script to monitor these system resources.

What is a Shell Script?

A shell script is a program that runs on a command-line interface and automates repetitive tasks. It is a series of commands written in a scripting language, such as Bash, that can be executed in the Linux terminal.

Creating a Shell Script

To create a shell script, we first need to create a file with a .sh extension. For example, let's create a file called sysmon.sh using the nano editor:

nano sysmon.sh

This will open the nano editor, where we can write our shell script. Once we have written our script, we can save and close the editor by pressing Ctrl + X, then Y, and finally Enter.

The next step is to make the script executable by changing its permission with the following command:

chmod +x sysmon.sh

Now that our script is executable, let's move on to monitoring system resources.

Monitoring Network Usage

To monitor network usage, we can use the iftop command-line utility. This tool displays real-time network usage by monitoring network connections and displaying bandwidth usage per connection. To use iftop in our script, we need to install it using the following command:

sudo apt-get install iftop

After installing iftop, we can use it in our script to monitor network usage with the following command:

iftop -t -s 2

This command displays network usage every two seconds in real-time, as shown below:

22:46:25     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:27     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:29     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:31     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
22:46:33     0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

Monitoring Disk Usage

To monitor disk usage, we can use the df command-line utility. This tool displays file system disk space usage by partition. To use the df command in our script, we can use the following command:

df -h

This command displays disk usage in human-readable format, as shown below:

Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           798M  1.1M  797M   1% /run
/dev/sda1        93G  9.9G   78G  12% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop1       56M   56M     0 100% /snap/core18/2128
/dev/loop2       33M   33M     0 100% /snap/snapd/14788

Monitoring Uptime

To monitor system uptime, we can use the uptime command-line utility. This tool displays the current time, system uptime, number of users, and system load average over the last 1, 5, and 15 minutes. To use the uptime command in our script, we can use the following command:

uptime

This command displays system uptime and load average, as shown below:

22:56:16 up 15 days,  8:43,  1 user,  load average: 0.00, 0.01, 0.05

Monitoring Load Average

To monitor system load average, we can use the top command-line utility. This tool displays real-time system resource usage, including CPU usage, memory usage, and system load average. To use the top command in our script, we can use the following command:

top -bn1 | grep load | awk '{printf "%.2f<br>", $(NF-2)}'

This command displays the 1-minute load average, as shown below:

0.00

Monitoring RAM Usage

To monitor RAM usage, we can use the free command-line utility. This tool displays total, used, and free memory available on the system. To use the free command in our script, we can use the following command:

free -m | awk 'NR==2{printf "%.2f%%<br>", $3*100/$2 }'

This command displays the percentage of used RAM, as shown below:

35.00%

Combining Commands into a Complete Script

Now that we have commands to monitor network usage, disk usage, uptime, load average, and RAM usage, we can combine them into a comprehensive shell script. Our final script will look like the following:

System Monitoring Script Structure #!/bin/bash (Shebang) Network Usage Monitoring Disk Usage Monitoring Uptime & Load Average RAM Usage Monitoring Output Formatting & Display

#!/bin/bash

echo "========================================"
echo "       SYSTEM MONITORING REPORT        "
echo "========================================"
echo "Report generated on: $(date)"
echo ""

echo "Network Usage:"
sudo iftop -t -s 2
echo ""

echo "Disk Usage:"
df -h
echo ""

echo "System Uptime:"
uptime
echo ""

echo "1-Minute Load Average:"
top -bn1 | grep load | awk '{printf "%.2f<br>", $(NF-2)}'
echo ""

echo "RAM Usage:"
free -m | awk 'NR==2{printf "Memory Usage: %.2f%%<br>", $3*100/$2 }'
echo "========================================"

Running the Script

To run this script, navigate to the directory where you saved the file and execute it with the following command:

./sysmon.sh

This will run the script and display system resource metrics in the terminal.

Automating with Cron Jobs

The script can be scheduled to run automatically at regular intervals using a cron job. A cron job is a scheduled task that runs at specified intervals and can be used to automate repetitive tasks. To schedule the script to run every 10 minutes, we can add the following entry to the crontab file:

*/10 * * * * /path/to/sysmon.sh >> /var/log/sysmon.log 2>&1

This entry will run the script every 10 minutes and redirect the output to a log file for analysis.

Additional Customizations

  • Network filtering ? Modify network monitoring to show traffic from specific interfaces or IP addresses

  • Disk filtering ? Filter disk usage to show only specific partitions or directories

  • Email alerts ? Send notifications when resource usage exceeds predefined thresholds

  • Log rotation ? Implement log rotation to prevent log files from growing too large

Conclusion

Shell scripting is a powerful tool for automating system monitoring in Linux environments. By combining essential commands like iftop, df, uptime, and free, system administrators can create comprehensive monitoring solutions that save time and ensure system health. The script can be customized and scheduled using cron jobs for continuous monitoring.

Updated on: 2026-03-17T09:01:38+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements