Operations 6 min read

Essential Shell Scripts Every Ops Engineer Should Use

This article presents a collection of practical Bash scripts for system administrators, covering load monitoring, file backup, log cleanup, service health checks, automated deployment, disk usage alerts, temporary file removal, network connectivity testing, bulk renaming, and batch service control, each with ready-to-use code examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Essential Shell Scripts Every Ops Engineer Should Use

When working on frontline operations, having a set of ready-to-use shell scripts can greatly improve efficiency and reliability. Below are ten common Bash scripts that address typical tasks in system administration.

System load monitoring : Checks the system load using uptime and sends an email alert if the load exceeds a defined threshold.

#!/bin/bash
threshold=1.0
load=$(uptime | awk -F'[, ]+' '{print $(NF-2)}')
if (( $(echo "$load > $threshold" | bc -l) )); then
    echo "System load too high: $load" | mail -s "Load Alert" [email protected]
fi

File backup : Periodically backs up a specified directory using cp and a cron job.

#!/bin/bash
backup_dir="/path/to/backup"
source_dir="/path/to/source"
timestamp=$(date +%Y%m%d%H%M%S)
backup_file="backup_${timestamp}.tar.gz"
tar czf "${backup_dir}/${backup_file}" "${source_dir}"

Log file cleanup : Removes log files older than a configurable number of days using find .

#!/bin/bash
log_dir="/path/to/logs"
days_to_keep=7
find "$log_dir" -type f -name "*.log" -mtime +$days_to_keep -delete

Service status monitoring : Checks whether a critical service (e.g., nginx ) is active with systemctl and emails an alert if it is not.

#!/bin/bash
service_name="nginx"
if ! systemctl is-active --quiet "$service_name"; then
    echo "Service $service_name is not running" | mail -s "Service Alert" [email protected]
fi

Automated deployment : Uses rsync in a loop to copy files to multiple servers.

#!/bin/bash
servers=("server1" "server2" "server3")
source_dir="/path/to/source"
destination_dir="/path/to/destination"
for server in "${servers[@]}"; do
    rsync -avz "$source_dir" "$server:$destination_dir"
done

Disk space monitoring : Parses df -h output and sends an email when usage exceeds a set percentage.

#!/bin/bash
threshold=90
df_output=$(df -h)
while read -r line; do
    usage=$(echo "$line" | awk '{print $5}' | sed 's/%//')
    if (( usage > threshold )); then
        echo "Disk space low: $line" | mail -s "Disk Alert" [email protected]
    fi
done <<< "$df_output"

Temporary file cleanup : Deletes files in a temporary directory older than a defined number of days.

#!/bin/bash
temp_dir="/path/to/temp"
expiration_days=3
find "$temp_dir" -type f -mtime +$expiration_days -delete

Network connectivity monitoring : Pings a critical IP address and alerts if the host is unreachable.

#!/bin/bash
service_ip="192.168.0.1"
if ! ping -c 1 -W 1 "$service_ip" > /dev/null; then
    echo "Cannot reach service: $service_ip" | mail -s "Network Alert" [email protected]
fi

Batch file renaming : Renames files in a directory with a sequential prefix.

#!/bin/bash
directory="/path/to/files"
prefix="new_file"
count=1
for file in "$directory"/*; do
    new_file_name="$directory/${prefix}${count}"
    mv "$file" "$new_file_name"
    ((count++))
 done

Start/stop multiple services : Loops over a list of services and runs systemctl start or systemctl stop on each.

#!/bin/bash
services=("service1" "service2" "service3")
action="start"  # or "stop"
for service in "${services[@]}"; do
    systemctl "$action" "$service"
done

These examples illustrate common operational needs; they can be adapted and extended to fit specific environments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationOpsLinuxShell
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.