Server management often involves repetitive tasks -- monitoring disk space, checking running processes, managing backups, and restarting services.
With shell scripting, you can automate these routine jobs, save time, and reduce human error.
In this blog, we will go through 10 practical shell scripts every system administrator or DevOps engineer should have in their toolkit.
Check Disk Usage and Alert When Full
This script checks if your disk usage exceeds a threshold (e.g., 80%) and sends an alert.
#!/bin/bash
threshold=80
used=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ "$used" -ge "$threshold" ]; then
echo "Disk usage is above $threshold%! Used: $used%"
# Send mail or Slack alert here
fi
Use Case: Monitor servers for low disk space to prevent crashes.
Monitor System Uptime
Simple script to log system uptime daily.
#!/bin/bash
echo "$(date): $(uptime -p)" >> /var/log/system_uptime.log
Use Case: Keep a record of uptime for performance analysis or reporting.
Backup a Directory Automatically
Automate periodic backups of important folders.
#!/bin/bash
src="/var/www/html"
dest="/backup/html_$(date +%F).tar.gz"
tar -czf "$dest" "$src"
echo "Backup created at $dest"
Use Case: Automatically back up web or app files daily.
Restart a Service if It Stops
Monitor and restart critical services like Apache or MySQL.
#!/bin/bash
service="apache2"
if ! pgrep -x "$service" >/dev/null; then
echo "$service is down! Restarting..."
systemctl restart "$service"
fi
Use Case: Ensure high availability of web or database services.
Clean Temporary Files
Remove old log and temp files to free up space.
#!/bin/bash
find /tmp -type f -mtime +7 -exec rm -f {} \;
echo "Old temp files deleted."
Use Case: Keeps your system clean and prevents clutter.
Check Active Connections
Display top IPs connected to your server.
#!/bin/bash
netstat -tn 2>/dev/null | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
Use Case: Identify potential attacks or heavy users.
CPU and Memory Usage Report
Quickly check CPU and RAM stats.
#!/bin/bash
echo "===== CPU USAGE ====="
top -bn1 | grep "Cpu(s)"
echo "===== MEMORY USAGE ====="
free -h
Use Case: Useful for periodic performance snapshots.
Automate System Updates
Keep your server secure with regular updates.
#!/bin/bash
apt update && apt upgrade -y
echo "System updated on $(date)" >> /var/log/update.log
Use Case: Schedule this in cron for weekly updates.
Monitor User Logins
Track who logs into your server.
#!/bin/bash
last -a | head -n 10
Use Case: Detect unauthorized or suspicious access.
Auto-Reboot After Kernel Update
Reboot only if required after updates.
#!/bin/bash
if [ -f /var/run/reboot-required ]; then
echo "Reboot required. Rebooting now..."
reboot
fi
Use Case: Avoid downtime after kernel updates by automating restarts safely.
Wrapping Up
These 10 shell scripts cover common server management tasks:
- System monitoring
- Backups
- Performance reporting
- Automated maintenance
You can schedule these scripts using cron jobs to run daily, weekly, or monthly — making your servers self-managing and more reliable.