System monitoring is a crucial aspect of maintaining and optimizing Unix-like systems. Bash provides powerful tools and commands for monitoring various system resources, processes, and performance metrics. This guide will introduce you to essential Bash system monitoring techniques.
The top
command provides a real-time, dynamic view of system processes. It displays CPU usage, memory consumption, and other vital statistics.
top
htop
is an enhanced version of top
with a more user-friendly interface and additional features.
htop
The free
command displays information about system memory usage, including total, used, and available memory.
free -h
Use df
to check disk space usage on mounted file systems.
df -h
iostat
reports CPU statistics and input/output statistics for devices and partitions.
iostat -x 2
Bash scripting allows you to create custom monitoring solutions tailored to your specific needs. Here's a simple example that monitors CPU usage:
#!/bin/bash
while true; do
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
echo "CPU Usage: $cpu_usage%"
sleep 5
done
This script uses Bash While Loops to continuously monitor CPU usage and display it every 5 seconds.
For more advanced system monitoring, consider exploring these topics:
By mastering these Bash system monitoring techniques, you'll be well-equipped to maintain optimal performance and quickly identify potential issues in your Unix-like systems.