Bash Scripting: Create a Simple System Resource Monitor
Learn how to create a powerful bash script that monitors CPU, memory, and disk usage on your Linux system. This tutorial walks you through writing a simple yet effective system resource monitor, perfect for system administrators and Linux enthusiasts looking to keep track of their server's performance. You can use these elements in your blog post metadata or CMS. The title is catchy and descriptive, the slug is SEO-friendly and easy to read in a URL, and the excerpt provides a concise summary of what readers can expect from the article.

Creating a Simple System Monitoring Script in Bash

Introduction

In this post, we'll create a bash script that monitors system resources and logs the information. This script will be useful for system administrators who need to keep track of server performance.

The Script

Here's our bash script that monitors CPU, memory, and disk usage:

#!/bin/bash

# File to log the results
LOG_FILE="/var/log/system_monitor.log"

# Function to get CPU usage
get_cpu_usage() {
    top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
}

# Function to get memory usage
get_memory_usage() {
    free | grep Mem | awk '{print $3/$2 * 100.0"%"}'
}

# Function to get disk usage
get_disk_usage() {
    df -h | grep '/dev/sda1' | awk '{print $5}'
}

# Main monitoring function
monitor_system() {
    local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
    local cpu_usage=$(get_cpu_usage)
    local memory_usage=$(get_memory_usage)
    local disk_usage=$(get_disk_usage)

    echo "$timestamp - CPU: $cpu_usage, Memory: $memory_usage, Disk: $disk_usage" >> "$LOG_FILE"
}

# Run the monitoring function
monitor_system

# Optional: Set up a cron job to run this script every 5 minutes
# */5 * * * * /path/to/this/script.sh

How It Works

  1. We define a LOG_FILE variable to specify where the monitoring data will be stored.
  2. Three functions are created to gather system information:
    • get_cpu_usage(): Uses top to get CPU idle time, then calculates usage.
    • get_memory_usage(): Uses free to calculate the percentage of used memory.
    • get_disk_usage(): Uses df to get the usage of the root partition.
  3. The monitor_system() function:
    • Gets the current timestamp.
    • Calls the three resource-gathering functions.
    • Logs the information to the specified file.
  4. Finally, we call monitor_system() to execute the script.

     

Usage

  1. Save the script to a file (e.g., system_monitor.sh).
  2. Make it executable:

    chmod +x system_monitor.sh
  3. Run the script:

    ./system_monitor.sh

Setting Up a Cron Job

To run this script automatically every 5 minutes:

  1. Open the crontab file:

    crontab -e
  2. Add the following line:

    */5 * * * * /path/to/system_monitor.sh
  3. Save and exit.

Conclusion

This simple bash script provides a foundation for system monitoring. You can expand it to include more checks, send alerts, or integrate with other monitoring tools.


For more on bash scripting, check out this tutorial:
 

175
Share this
Leave a comment
There are no comments about this article, let us know what you think?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.