How to Monitor Linux Server Performance with Simple Shell Scripts

Monitoring Linux server performance doesn’t require expensive tools or complex setups. You can build lightweight, effective monitoring scripts using built-in Linux commands like `top`, `free`, `df`, and `uptime`. These scripts give you real-time visibility into CPU load, memory usage, disk space, and uptime—all without third-party dependencies. In this guide, you’ll learn to create, automate, and customize simple shell scripts that keep your servers healthy, alert you to problems, and save you hours of manual checks. Perfect for DevOps engineers and sysadmins managing multiple servers.

## Why Use Shell Scripts for Server Monitoring?

Shell scripts are fast, portable, and require zero installation. They work on any Linux distribution, from Ubuntu to CentOS. Key benefits:

  • Lightweight – Uses minimal system resources
  • Customizable – Tailor exactly what you need
  • Automation-ready – Integrate with cron, systemd, or CI/CD
  • Cost-free – No licensing or subscription fees

> Pro tip: Combine scripts with email or Slack alerts for proactive monitoring.

Linux terminal showing real-time server performance monitoring script output

Photo by Digital Buggu on Pexels

## Essential Linux Commands for Monitoring

Before writing scripts, know these core commands:

Command What It Monitors Key Output
`top` CPU & memory usage per process Load average, %CPU, %MEM
`free -h` RAM usage (human-readable) Total, used, available
`df -h` Disk space per partition Used, available, usage%
`uptime` System uptime & load average 1, 5, 15-minute load
`vmstat` System processes, memory, paging Procs, memory, swap
`iostat` CPU & I/O statistics Device utilization

## Building Your First Monitoring Script

### Step 1: Create the Script File

“`bash

#!/bin/bash

# server_monitor.sh – Basic performance snapshot

echo “=== Server Performance Report ===”

echo “Date: $(date)”

echo “Uptime: $(uptime -p)”

echo “”

echo “— CPU Load —“

top -bn1 | grep “load average:” | awk ‘{print $10 $11 $12}’

echo “— Memory Usage —“

free -h | grep “Mem:” | awk ‘{print “Used: “$3 ” / Free: “$4}’

echo “— Disk Usage —“

df -h / | tail -1 | awk ‘{print “Used: “$3 ” / Avail: “$4 ” (“$5″)”}’

“`

Save as `server_monitor.sh` and make executable:

`chmod +x server_monitor.sh`

### Step 2: Run It

“`bash

./server_monitor.sh

“`

You’ll see a clean report like:

“`

=== Server Performance Report ===

Date: Mon Mar 17 10:23:45 UTC 2025

Uptime: up 14 days, 3 hours

— CPU Load —

load average: 0.45, 0.62, 0.78

— Memory Usage —

Used: 3.2G / Free: 1.1G

— Disk Usage —

Used: 45G / Avail: 120G (27%)

“`

Terminal output of a server monitoring shell script showing CPU, memory, and disk usage

Photo by Digital Buggu on Pexels

## Advanced Monitoring Scripts

### 1. Real-Time CPU Alert Script

“`bash

#!/bin/bash

# cpu_alert.sh – Warns if CPU load exceeds threshold

THRESHOLD=80

CPU_LOAD=$(top -bn1 | grep “Cpu(s)” | awk ‘{print $2}’ | cut -d’.’ -f1)

if [ “$CPU_LOAD” -gt “$THRESHOLD” ]; then

echo “WARNING: CPU load is ${CPU_LOAD}% (threshold: ${THRESHOLD}%)”

# Add alert command here (e.g., mail, Slack webhook)

else

echo “OK: CPU load is ${CPU_LOAD}%”

fi

“`

Customize the threshold by changing `THRESHOLD=80` to your desired percentage.

### 2. Disk Space Monitoring with Alerts

“`bash

#!/bin/bash

# disk_monitor.sh – Checks disk usage and alerts if over 90%

ALERT=90

PARTITION=”/”

USAGE=$(df -h “$PARTITION” | tail -1 | awk ‘{print $5}’ | sed ‘s/%//’)

if [ “$USAGE” -gt “$ALERT” ]; then

echo “CRITICAL: Disk usage on $PARTITION is ${USAGE}%”

# Trigger cleanup or notification

else

echo “Disk usage on $PARTITION is ${USAGE}% – safe”

fi

“`

### 3. Memory Usage Trend Script

“`bash

#!/bin/bash

# mem_trend.sh – Logs memory usage every 5 seconds for 1 minute

echo “Time,Used_MB,Free_MB” > mem_log.csv

for i in {1..12}; do

USED=$(free -m | grep “Mem:” | awk ‘{print $3}’)

FREE=$(free -m | grep “Mem:” | awk ‘{print $4}’)

echo “$(date +%H:%M:%S),$USED,$FREE” >> mem_log.csv

sleep 5

done

echo “Data saved to mem_log.csv”

“`

Use this to debug memory leaks or plan capacity.

## Automating Your Monitoring Scripts

### Using Cron for Scheduled Checks

Run your script every 5 minutes:

“`bash

crontab -e

“`

Add this line:

“`

*/5 * * * * /home/user/server_monitor.sh >> /var/log/server_monitor.log 2>&1

“`

Best practices:

  • Log output to a file for later review
  • Use absolute paths in cron jobs
  • Test with a manual run first

### Sending Alerts via Email

Install `mailutils` (Debian/Ubuntu):

“`bash

sudo apt install mailutils -y

“`

Modify your alert script:

“`bash

if [ “$CPU_LOAD” -gt “$THRESHOLD” ]; then

echo “High CPU load: ${CPU_LOAD}%” | mail -s “Server Alert” [email protected]

fi

“`

### Slack Webhook Integration

Replace the email line with a curl command:

“`bash

curl -X POST -H ‘Content-type: application/json’ \

–data ‘{“text”:”CPU load is high: ‘”$CPU_LOAD%”‘”}’ \

https://hooks.slack.com/services/YOUR/WEBHOOK/URL

“`

Diagram showing cron job triggering a shell script that sends alerts to email and Slack

Photo by cottonbro studio on Pexels

## Comparison: Shell Scripts vs. Monitoring Tools

Feature Shell Scripts Nagios/Zabbix Prometheus
Setup time 5 minutes Hours Hours
Resource usage Minimal Moderate Moderate
Customization Full control Template-based Config-based
Scaling Manual per server Centralized Centralized
Cost Free Free/Paid Free
Learning curve Low Medium High

When to use scripts: Quick setups, small infrastructures, single-server monitoring, or as a supplement to existing tools.

## Pro Tips for Production Use

1. Use exit codes – `exit 0` for OK, `exit 1` for warning, `exit 2` for critical

2. Add timestamps – Always include `$(date)` in logs

3. Limit output – Use `tail -n 10` to avoid flooding logs

4. Test thresholds – Simulate high load with `stress` command

5. Secure scripts – Set permissions to `750` and avoid running as root unless necessary

## Troubleshooting Common Issues

Script not executable?

Run `chmod +x script.sh`

Command not found errors?

Install required packages: `sudo apt install sysstat procps`

Cron job not running?

Check cron logs: `grep CRON /var/log/syslog`

Email alerts not sending?

Verify mail server setup: `echo “test” | mail -s “test” [email protected]`

## Conclusion

Monitoring Linux server performance with simple shell scripts is a practical, cost-effective, and highly customizable approach for DevOps engineers and sysadmins. You’ve learned to build CPU, memory, and disk monitors, automate them with cron, and send alerts via email or Slack. These scripts give you immediate visibility into server health without overhead.

Ready to level up your automation toolkit?

Visit freefile.store to download ready-to-use developer scripts, DevOps tools, and automation templates. Our library includes advanced monitoring scripts, deployment helpers, and CI/CD integrations—all free to download. Start optimizing your servers today!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *