Monitor CPU and RAM Usage
Get notified when your server is struggling — before users notice.
You deployed your app, set up cron, and walked away. Three hours later your server is at 97% CPU and you find out from a user. This script runs silently on a schedule and logs a warning the moment resources spike past your threshold.
Works on any Linux system — Ubuntu, Debian, CentOS. Uses top and free, which are available on every distro by default. No dependencies to install.
The Script
Paste this into monitor.sh. Adjust THRESHOLD and LOG_FILE if you want a different cutoff or log path.
#!/bin/bash CHECK="✓" CROSS="✗" # --- Configuration --- THRESHOLD=80 # Alert when usage exceeds this % LOG_FILE="/var/log/resource-monitor.log" DATE=$(date '+%Y-%m-%d %H:%M:%S') # --- CPU Usage --- CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1 | cut -d',' -f1 | xargs printf "%.0f") # --- RAM Usage --- RAM=$(free | awk '/Mem:/ {printf "%.0f", $3/$2*100}') echo "[$DATE] CPU: ${CPU}% | RAM: ${RAM}%" # --- CPU Alert --- if [ "$CPU" -gt "$THRESHOLD" ]; then echo "$CROSS [$DATE] WARNING: CPU at ${CPU}% (threshold: ${THRESHOLD}%)" | tee -a "$LOG_FILE" else echo "$CHECK CPU OK: ${CPU}%" fi # --- RAM Alert --- if [ "$RAM" -gt "$THRESHOLD" ]; then echo "$CROSS [$DATE] WARNING: RAM at ${RAM}% (threshold: ${THRESHOLD}%)" | tee -a "$LOG_FILE" else echo "$CHECK RAM OK: ${RAM}%" fi
How It Works
| Line / piece | What it does |
|---|---|
| THRESHOLD=80 | The percentage at which a warning fires. |
| top -bn1 | Runs top in batch mode for one iteration (non-interactive). |
| awk '{print $2}' | Extracts the CPU usage column from top output. |
| free | awk '/Mem:/ ...' | Parses free output to calculate RAM used / total × 100. |
| tee -a "$LOG_FILE" | Prints to stdout and appends to the log file. |
| $DATE | Timestamp so every log entry is traceable. |
How to Run
nano monitor.sh- Paste the script. Ctrl+X → Y → Enter to save.
chmod +x monitor.sh- Test:
./monitor.sh - Schedule:
crontab -e→ add:0 * * * * /home/user/monitor.sh
To match the HowTo schema and keep a paper trail, append: >> /var/log/monitor.log 2>&1 to your cron line (use a path you can write to, or run the job as a user that owns the log).
Variations
Email when a threshold trips
Pipe the warning line to mail: ... | mail -s "CPU Alert" you@example.com (install mailutils on Debian/Ubuntu if needed).
Check a specific process
Use per-process CPU with something like: ps aux | grep processname | awk '{sum+=$3} END {print sum}' (filter grep so you don’t count the grep line itself).
Feed a dashboard
Append JSON lines (timestamp, CPU, RAM) to a file and graph them with a small script or external tool.
Frequently Asked Questions
How do I check CPU usage in bash?
Use top in batch mode: top -bn1 | grep 'Cpu(s)'. For idle as a number, pipe through awk: top -bn1 | grep 'Cpu(s)' | awk '{print $8}'
How do I check RAM usage in Linux?
Use free: free -h is human-readable. In a script: free | awk '/Mem:/ {printf "%.0f", $3/$2*100}' gives RAM usage as a percentage.
How do I get an alert when CPU is too high?
Capture CPU use in a script, compare to a threshold with an if, then schedule the script with cron every minute or hour (or use email from the variations above).
What is a normal CPU usage percentage on a Linux server?
Under 70% is generally healthy for production. Sustained 80–90%+ usually means something worth inspecting with top or htop.
How do I find which process is using the most CPU?
Run: ps aux --sort=-%cpu | head -10 for the top 10. Or run top interactively and press P to sort by CPU.