๐Ÿš€ CPU & Memory Monitoring Script

Overview

This project provides a Bash script to monitor CPU and Memory usage on a Linux server. The script logs high resource usage and helps in proactive system administration.

๐Ÿ› ๏ธ Prerequisites

Ensure your Linux system suppor…


This content originally appeared on DEV Community and was authored by Kishore Suzil

Overview

This project provides a Bash script to monitor CPU and Memory usage on a Linux server. The script logs high resource usage and helps in proactive system administration.

๐Ÿ› ๏ธ Prerequisites

Ensure your Linux system supports the following commands:

  • ps for listing processes
  • df for checking disk usage
  • free for monitoring memory
  • uptime for system uptime details

๐Ÿ“ Bash Script: monitor_usage.sh

#!/bin/bash

# Define CPU and Memory usage thresholds
CPU_THRESHOLD=80.0  # Stop if any process exceeds 80% CPU usage
MEM_THRESHOLD=70.0  # Stop if any process exceeds 70% memory usage

# Print current time and system uptime
echo "Current time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "System uptime: $(uptime)"

# Display disk usage
echo "Disk usage:"
df -h --output=source,pcent | grep -v "Use%"

# Display available memory
echo "Free memory: $(free -m | awk 'NR==2 {print $4}') MB"

# Display system load average
echo "Load average: $(cat /proc/loadavg | awk '{print $1,$2,$3}')"

echo "-----------------------------------------------"

# Check for high CPU usage
HIGH_CPU_PROCESS=$(ps -eo pid,%cpu,cmd --sort=-%cpu | awk -v threshold="$CPU_THRESHOLD" '$2>threshold {print $1, $2, $3; exit}')
if [[ ! -z "$HIGH_CPU_PROCESS" ]]; then
  echo "โŒ High CPU Usage Detected: $HIGH_CPU_PROCESS"
  echo "$(date) - High CPU Usage: $HIGH_CPU_PROCESS" >> high_usage_log.txt
fi

# Check for high memory usage
HIGH_MEM_PROCESS=$(ps -eo pid,%mem,cmd --sort=-%mem | awk -v threshold="$MEM_THRESHOLD" '$2>threshold {print $1, $2, $3; exit}')
if [[ ! -z "$HIGH_MEM_PROCESS" ]]; then
  echo "โŒ High Memory Usage Detected: $HIGH_MEM_PROCESS"
  echo "$(date) - High Memory Usage: $HIGH_MEM_PROCESS" >> high_usage_log.txt
fi

echo "-----------------------------------------------"

# Exit the script after completing one run
exit 0

๐Ÿ”„ Running the Script

  1. Make the script executable:
   chmod +x monitor_usage.sh
  1. Run the script:
   ./monitor_usage.sh

๐Ÿ“Š Summary

โœ… Monitors CPU & Memory usage

โœ… Logs high resource-consuming processes


This content originally appeared on DEV Community and was authored by Kishore Suzil


Print Share Comment Cite Upload Translate Updates
APA

Kishore Suzil | Sciencx (2025-03-02T13:29:58+00:00) ๐Ÿš€ CPU & Memory Monitoring Script. Retrieved from https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/

MLA
" » ๐Ÿš€ CPU & Memory Monitoring Script." Kishore Suzil | Sciencx - Sunday March 2, 2025, https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/
HARVARD
Kishore Suzil | Sciencx Sunday March 2, 2025 » ๐Ÿš€ CPU & Memory Monitoring Script., viewed ,<https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/>
VANCOUVER
Kishore Suzil | Sciencx - » ๐Ÿš€ CPU & Memory Monitoring Script. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/
CHICAGO
" » ๐Ÿš€ CPU & Memory Monitoring Script." Kishore Suzil | Sciencx - Accessed . https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/
IEEE
" » ๐Ÿš€ CPU & Memory Monitoring Script." Kishore Suzil | Sciencx [Online]. Available: https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/. [Accessed: ]
rf:citation
» ๐Ÿš€ CPU & Memory Monitoring Script | Kishore Suzil | Sciencx | https://www.scien.cx/2025/03/02/%f0%9f%9a%80-cpu-memory-monitoring-script/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.