Bash script system monitoring information like CPU usage, memory usage, and disk space:

Опубликовано: 13 Октябрь 2024
на канале: BASH SCRIPTING & PROGRAMMING
390
6

Here's a simple Bash script that provides basic system monitoring information like CPU usage, memory usage, and disk space:

bash source code:
----------------------------------------------------------------------------------------------------------------------
#!/bin/bash

echo "System Monitor"

Get CPU usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')

Get memory usage
memory_usage=$(free | awk '/Mem/{printf("%.2f"), $3/$2 * 100}')

Get disk space usage
disk_usage=$(df -h / | awk 'NR==2 {print $5}')

echo "CPU Usage: $cpu_usage%"
echo "Memory Usage: $memory_usage%"
echo "Disk Space Usage: $disk_usage used"
----------------------------------------------------------------------------------------------------------------------

This script uses several commands like (top, free, and df) to gather CPU usage, memory usage, and disk space usage information respectively.

To execute the script, follow these steps:

1: Open a text editor.
2: Copy and paste the script into the editor.
3: Save the file with a .sh extension (e.g., system_monitor.sh).
4: Make the file executable with the command:
4.1: chmod +x system_monitor.sh.
5: Run the script using ./system_monitor.sh.

Keep in mind that this script provides a basic overview.
For a more comprehensive system monitoring solution, various tools like (htop, glances, or nmon) offer more detailed insights into system resources.