The Script

Paste this into monitor.sh. Adjust THRESHOLD and LOG_FILE if you want a different cutoff or log path.

monitor.sh
#!/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 / pieceWhat it does
THRESHOLD=80The percentage at which a warning fires.
top -bn1Runs 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.
$DATETimestamp so every log entry is traceable.

How to Run

  1. nano monitor.sh
  2. Paste the script. Ctrl+X → Y → Enter to save.
  3. chmod +x monitor.sh
  4. Test: ./monitor.sh
  5. Schedule: crontab -e → add: 0 * * * * /home/user/monitor.sh
💡 Log cron output

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.

Run this monitor on a server that's always watching Monitoring only works if it's running 24/7. A $4/mo DigitalOcean Droplet gives you a dedicated Linux environment for cron jobs, monitoring scripts, and backups — always on, never sleeping. New accounts get $200 in credits.
Start your Droplet →

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.

Related Snippets