This Bash script collects system metrics, clears caches, removes old log files, and generates a detailed report. The report is saved in a file named report.txt.
- Captures system metrics before and after cleanup.
- Clears system caches.
- Identifies top memory-consuming processes.
- Identifies the top space-consuming files.
- Deletes log files older than one week.
REPORT="report.txt" # File to store the system metrics report
LOG_DIR="/var/log" # Directory containing log filesThe capture_metrics function gathers system information including:
- Free memory
- CPU utilization
- Disk space usage
capture_metrics() {
echo "=== $1 ===" >> "$REPORT" # Section header
echo "Free Memory:" >> "$REPORT"
free -h >> "$REPORT" # Displays memory status in a human-readable format
echo "CPU Utilization:" >> "$REPORT"
top -b -n1 | grep "Cpu(s)" >> "$REPORT" # Extracts CPU usage summary
echo "Disk Space Usage:" >> "$REPORT"
df -h >> "$REPORT" # Displays disk space usage
echo "" >> "$REPORT" # Adds a blank line for readability
}Before making any changes, the script captures system metrics:
capture_metrics "Before Clearing Caches and Logs"echo "Clearing system caches..."
sudo su -c "free -h && sync && echo 3 > /proc/sys/vm/drop_caches && free -h" >> "$REPORT"This clears:
- Page cache
- Dentries
- Inodes
echo "=== Top 10 Memory-Consuming Processes ===" >> "$REPORT"
ps aux --sort=-%mem | head -n 10 >> "$REPORT"Lists the top 10 processes consuming the most memory.
echo "=== Top 10 Space-Consuming Files ===" >> "$REPORT"
find / -type f -exec du -h {} + | sort -rh | head -n 10 >> "$REPORT"Finds the 10 largest files on the system.
echo "Removing old log files..."
sudo find "$LOG_DIR" -type f -name "*.log" -mtime +7 -exec rm -rf {} \;Deletes log files older than one week.
capture_metrics "After Clearing Caches and Old Logs"Captures system metrics again after the cleanup.
echo "=== Processor Usage ===" >> "$REPORT"
top -b -n1 | head -n 10 >> "$REPORT"Displays a processor usage summary.
echo "Logs older than 1 week cleared. Caches cleared. Report saved to $REPORT."Indicates the successful execution of the script.
- Save the script as
system_metrics.sh. - Give execution permission:
chmod +x system_metrics.sh
- Run the script:
sudo ./system_metrics.sh
- The script requires
sudoprivileges to clear caches and delete logs. - Be cautious when removing files, as incorrect modifications can delete important data.
- Modify
LOG_DIRto target different log directories if necessary.