In modern server Operation and Maintenance (O&M), Shell scripting remains a core skill for system administrators and DevOps engineers. Well-designed scripts automate repetitive tasks, reduce human error, and provide fast operational feedback when systems deviate from expected behavior.
Below are 10 practical and production-oriented Shell scripts commonly used in daily server management.
🩺 Server Health Check Script #
This script performs a quick health snapshot of the server, including CPU, memory, disk usage, and active network connections. It is ideal for scheduled execution via cron.
#!/bin/bash
echo "===== Server Health Check ====="
echo "Check Time: $(date)"
echo "CPU Usage: $(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')%"
echo "Memory Usage: $(free -m | awk 'NR==2{printf "%.2f%%\n", $3*100/$2 }')"
echo "Disk Usage: $(df -h | awk '$NF=="/"{printf "%s\n", $5}')"
echo "Network Connections: $(netstat -an | grep ESTABLISHED | wc -l)"
echo "==============================="
🧹 Log Cleanup Script #
Log files grow continuously and can quickly consume disk space. This script removes .log files older than a defined retention period.
#!/bin/bash
LOG_DIR="/var/log"
DAYS_TO_KEEP=7
echo "Starting log cleanup..."
find $LOG_DIR -type f -name "*.log" -mtime +$DAYS_TO_KEEP -exec rm -f {} \;
echo "Cleaned logs older than $DAYS_TO_KEEP days."
echo "Current log directory size: $(du -sh $LOG_DIR | awk '{print $1}')"
💾 Automated Backup Script #
Data protection is critical in O&M. This script creates a compressed backup of a target directory and automatically purges backups older than 30 days.
#!/bin/bash
BACKUP_DIR="/backup"
SOURCE_DIR="/data"
DATE=$(date +%Y%m%d)
BACKUP_FILE="$BACKUP_DIR/backup_$DATE.tar.gz"
tar -czf $BACKUP_FILE $SOURCE_DIR
find $BACKUP_DIR -type f -name "*.tar.gz" -mtime +30 -exec rm -f {} \;
echo "Backup task completed!"
🌐 Network Connection Monitor #
This script analyzes established network connections, grouping them by remote IP to help identify abnormal traffic patterns.
#!/bin/bash
echo "Current Network Connections:"
netstat -an | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
echo "Total Connections: $(netstat -an | grep ESTABLISHED | wc -l)"
🔁 Service Auto-Restart Script #
Ensures that a critical service (such as Nginx) is always running. If the service stops, it is automatically restarted and logged.
#!/bin/bash
SERVICE="nginx"
LOG_FILE="/var/log/service_monitor.log"
if systemctl is-active --quiet $SERVICE; then
echo "$(date): $SERVICE is running." >> $LOG_FILE
else
echo "$(date): $SERVICE stopped. Restarting..." >> $LOG_FILE
systemctl restart $SERVICE
fi
🚨 Disk Space Alarm Script #
When disk usage exceeds a defined threshold, this script sends an alert and lists the largest files consuming space.
#!/bin/bash
THRESHOLD=90
USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
if [ $USAGE -gt $THRESHOLD ]; then
echo "Disk space critical: $USAGE%" | mail -s "Disk Alarm" admin@example.com
du -ah / | sort -rh | head -n 10
fi
🔄 Automatic System Update #
Automates package updates and records upgrade activity for audit and troubleshooting purposes.
#!/bin/bash
LOG_FILE="/var/log/system_update.log"
apt-get update && apt-get upgrade -y >> $LOG_FILE
systemctl list-units --state=needs-reloading
🧠 Process Monitor Script #
Monitors a specific process (for example, a Java application) and restarts it if it is not running.
#!/bin/bash
PROCESS="java"
if ! pgrep $PROCESS > /dev/null; then
systemctl restart $PROCESS
fi
⏱️ Time Synchronization Script #
Maintains accurate system time by synchronizing with a public NTP server, which is essential for logs, security, and distributed systems.
#!/bin/bash
ntpdate pool.ntp.org
if [ $? -eq 0 ]; then
echo "Time synced: $(date)"
else
echo "Sync failed!"
fi
🖥️ Batch Server Management Script #
Executes the same command across multiple remote servers via SSH, useful for inventory checks or quick diagnostics.
#!/bin/bash
SERVERS=("server1" "server2" "server3")
COMMAND="df -h"
for SERVER in "${SERVERS[@]}"; do
echo "===== $SERVER ====="
ssh $SERVER "$COMMAND"
done
These scripts form a solid operational baseline for Linux server environments. With small adaptations—such as better logging, error handling, or configuration files—they can be safely integrated into production workflows and automated scheduling systems.