Operations 7 min read

Essential Shell Automation Scripts for System Administration

This article presents a collection of practical Bash scripts that automate common system administration tasks such as disk usage monitoring, MySQL backups, service health checks, temporary file cleanup, resource monitoring, user creation, and subnet IP scanning, each with clear explanations and ready-to-use code.

DevOps Operations Practice
DevOps Operations Practice
DevOps Operations Practice
Essential Shell Automation Scripts for System Administration

1. Check Disk Usage

This script checks the system's disk usage and sends an email alert when usage exceeds a defined threshold.

#!/bin/bash
THRESHOLD=80
EMAIL="[email protected]"

df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
  partition=$(echo $output | awk '{ print $2 }')
  if [ $usep -ge $THRESHOLD ]; then
    echo "Warning: The partition \"$partition\" has used $usep% at $(date)" | mail -s "Disk Space Alert: $partition" $EMAIL
  fi
done

2. Automatic MySQL Database Backup

This script creates daily backups of a MySQL database, stores them in a designated directory, and removes backups older than seven days.

#!/bin/bash
BACKUP_DIR="/backup/mysql"
MYSQL_USER="root"
MYSQL_PASSWORD="password"
DATABASE_NAME="mydatabase"

# Create backup directory
mkdir -p $BACKUP_DIR

# Create a new backup
mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE_NAME > $BACKUP_DIR/$DATABASE_NAME-$(date +%F).sql

# Remove backup files older than seven days
find $BACKUP_DIR -type f -mtime +7 -exec rm {} \;

3. Check and Restart Failed Service

This script verifies whether a specified service (e.g., nginx) is running; if it is down, the script attempts to restart it and sends notification emails about the status.

#!/bin/bash
SERVICE="nginx"
EMAIL="[email protected]"

if ! systemctl is-active --quiet $SERVICE; then
  echo "$SERVICE is down. Attempting to restart..." | mail -s "$SERVICE is down" $EMAIL
  systemctl restart $SERVICE
  if systemctl is-active --quiet $SERVICE; then
    echo "$SERVICE was successfully restarted" | mail -s "$SERVICE restarted" $EMAIL
  else
    echo "Failed to restart $SERVICE" | mail -s "$SERVICE restart failed" $EMAIL
  fi
fi

4. Clean Temporary Files

This script removes files in the /tmp directory that have not been modified for more than seven days and deletes empty sub‑directories.

#!/bin/bash
TEMP_DIR="/tmp"
DAYS=7

find $TEMP_DIR -type f -mtime +$DAYS -exec rm -f {} \;
find $TEMP_DIR -type d -empty -delete

5. System Resource Monitoring Script

This script records CPU and memory usage every minute and appends the information to a log file.

#!/bin/bash
LOG_FILE="/var/log/system_monitor.log"

while true; do
  echo "$(date): CPU: $(top -bn1 | grep \"Cpu(s)\" | awk '{print $2 + $4}')% MEM: $(free -m | awk 'NR==2{printf \"%.2f%%\", $3*100/$2 }')" >> $LOG_FILE
  sleep 60
done

6. Add User and Grant Sudo Privileges

This script creates a new user, sets an initial password, and adds the user to the sudo (wheel) group.

#!/bin/bash
# 自动添加用户并授予sudo权限脚本

if [ -z "$1" ]; then
    echo "Usage: $0 <username>"
    exit 1
fi

USERNAME=$1
PASSWORD="initial_password"  # 可以修改初始密码

# Check if user already exists
if id "$USERNAME" &>/dev/null; then
    echo "用户 $USERNAME 已存在。"
    exit 1
fi

# Add user
useradd -m $USERNAME
if [ $? -ne 0 ]; then
    echo "添加用户 $USERNAME 失败。"
    exit 1
fi

# Set user password
echo "$USERNAME:$PASSWORD" | chpasswd
if [ $? -ne 0 ]; then
    echo "设置用户 $USERNAME 的密码失败。"
    exit 1
fi

# Grant sudo privileges (wheel group for Ubuntu)
usermod -aG wheel $USERNAME
if [ $? -ne 0 ]; then
    echo "添加用户 $USERNAME 到 sudo 组失败。"
    exit 1
fi

echo "用户 $USERNAME 已添加并授予 sudo 权限。"

7. Scan Subnet for Active IPs

This script scans all IP addresses in a given /24 subnet, pings each address, and reports which hosts respond.

#!/bin/bash
# 网段IP扫描脚本

if [ -z "$1" ]; then
    echo "Usage: $0 <subnet>"
    echo "Example: $0 192.168.1"
    exit 1
fi

SUBNET=$1

echo "开始扫描网段 $SUBNET.0/24 ..."
for i in {1..254}; do
    IP="$SUBNET.$i"
    ping -c 1 -W 1 $IP &>/dev/null
    if [ $? -eq 0 ]; then
        echo "IP $IP 存活"
    fi
done

echo "扫描完成。"
shellscriptingSystem Administration
DevOps Operations Practice
Written by

DevOps Operations Practice

We share professional insights on cloud-native, DevOps & operations, Kubernetes, observability & monitoring, and Linux systems.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.