Operations 6 min read

7 Practical Shell Scripts for Linux Automation

This article presents seven practical Bash shell scripts for Linux system administration, covering automatic file backup, disk usage alerts, old log cleanup, server reachability checks, DNS resolution monitoring, HTTP service health checks, and random password generation, each with explanations and code examples.

DevOps Operations Practice
DevOps Operations Practice
DevOps Operations Practice
7 Practical Shell Scripts for Linux Automation

Shell scripts are powerful tools for Linux system management and automation. By writing scripts, repetitive tasks can be automated, greatly improving efficiency.

1. Automatic backup of important files

Scenario: regularly backup the /home/user/documents directory to /backup and compress the archive.

#!/bin/bash
# 定义源目录和目标目录
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/backup"
DATE=$(date +%Y-%m-%d)

# 创建备份文件(带日期)
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR"

# 输出成功信息
echo "Backup completed: $BACKUP_DIR/backup_$DATE.tar.gz"

Explanation: tar -czf compresses the directory into a .tar.gz file; date +%Y-%m-%d provides the current date for the filename.

2. Disk usage alert

Scenario: send an email alert when disk usage exceeds 90%.

#!/bin/bash
THRESHOLD=90
CURRENT=$(df / | grep / | awk '{print $5}' | sed 's/%//g')

if [ "$CURRENT" -gt "$THRESHOLD" ]; then
    echo "Disk usage is $CURRENT% on $(hostname)" | mail -s "Disk Alert" [email protected]
fi

Explanation: df / gets root partition usage; awk '{print $5}' extracts the percentage; mail sends the alert.

3. Automatic cleanup of old log files

Scenario: delete log files in /var/log older than 30 days.

#!/bin/bash
find /var/log -type f -name "*.log" -mtime +30 -exec rm {} \;

Explanation: find -mtime +30 matches files modified more than 30 days ago; -exec rm {} \; removes them.

4. Server reachability check

Scenario: detect whether a list of servers is online.

#!/bin/bash
SERVERS=("192.168.1.1" "192.168.1.2" "example.com")

for server in "${SERVERS[@]}"; do
    ping -c 1 "$server" &> /dev/null && echo "$server is UP" || echo "$server is DOWN"
done

Explanation: ping -c 1 sends one ICMP request; the result is printed as UP or DOWN.

5. Real‑time DNS resolution monitoring

Scenario: continuously check if a domain resolves; log failures.

#!/bin/bash
domain="example.com"
log_file="dns.log"

while true; do
    result=$(dig +short $domain)
    [ -z "$result" ] && echo "$(date): DNS failed" >> $log_file
    sleep 1
done

Explanation: dig +short quickly returns the DNS result; an empty result triggers logging.

6. HTTP service monitoring script

Scenario: continuously test a website’s availability and record the HTTP status code.

#!/bin/bash
url="http://example.com"
log_file="http_test.log"

while true; do
    http_result=$(curl -s -o /dev/null -w "%{http_code}" $url)
    if [ $http_result -eq 200 ]; then
        echo "$(date): HTTP request to $url succeeded (Status: $http_result)" >> $log_file
    else
        echo "$(date): HTTP request to $url failed (Status: $http_result)" >> $log_file
    fi
    sleep 1
done

Explanation: curl -w "%{http_code}" fetches the HTTP status; 200 is logged as success.

7. Password generator

Scenario: generate a random password containing uppercase, lowercase, digits, and symbols.

#!/bin/bash
LENGTH=12
PASSWORD=$(tr -dc 'A-Za-z0-9!@#$%^&*' < /dev/urandom | head -c "$LENGTH")
echo "Generated Password: $PASSWORD"

Explanation: /dev/urandom provides random bytes; tr -dc filters the desired character set; head -c limits the length.

Backupsystem monitoringshell scriptingPassword generationLog cleanupDisk usage alertLinux automationNetwork health check
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

login 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.