Work Smarter, Not Harder: Bash Scripts for Daily Admin Tasks

Published in

on

In the fast-paced world of system administration, time is one of the most valuable resources. Repeating the same set of commands daily can lead to fatigue, errors, and inefficiency. The answer lies in automating your routine through the power of Bash scripting.

Bash

This article focuses on real-world, practical bash scripts that can help system administrators streamline their workflow, eliminate redundant tasks, and reduce operational risk.

Why Use Bash Scripts?

Bash (Bourne Again SHell) is a powerful command-line interface found on almost every Unix-like operating system. It allows admins to:

  • Automate repetitive tasks
  • Handle error checking
  • Chain multiple operations into a single script
  • Schedule tasks with cron
  • Improve consistency across operations

Let’s dive into some commonly required daily admin tasks and see how Bash can make your life easier.

1. System Health Check Script.

Purpose: Quickly inspect key health indicators of your system.

#!/bin/bash
echo "System Health Check - $(date)"
echo "Uptime:"
uptime
echo "Disk Usage:"
df -h
echo "Memory Usage:"
free -h
echo "Top 5 CPU Consuming Processes:"
ps aux --sort=-%cpu | head -n 6
echo "Top 5 Memory Consuming Processes:"
ps aux --sort=-%mem | head -n 6

Use Case: Save this script as healthcheck.sh, and run it every morning to get a quick status report of your system.

2. Log File Rotation and Cleanup.

Purpose: Prevent log files from consuming unnecessary disk space.

#!/bin/bash
LOG_DIR="/var/log/myapp"
DAYS=7
find "$LOG_DIR" -type f -name "*.log" -mtime +$DAYS -exec rm -f {} \;
echo "Old log files older than $DAYS days deleted from $LOG_DIR"

Use Case: Run this via cron weekly to keep your logs in check.

3. Backup Script for Critical Files.

Purpose: Automatically back up important configuration or content files.

#!/bin/bash
BACKUP_SRC="/etc /home /var/www"
BACKUP_DEST="/mnt/backup"
DATE=$(date +%F)
mkdir -p "$BACKUP_DEST"
tar -czf "$BACKUP_DEST/backup-$DATE.tar.gz" $BACKUP_SRC
echo "Backup completed on $DATE"

Use Case: Schedule it daily using cron to create timestamped backups.

4. User Account Audit.

Purpose: Monitor for unexpected user additions or changes.

#!/bin/bash
CURRENT_FILE="/tmp/current_users.txt"
PREVIOUS_FILE="/tmp/previous_users.txt"
cut -d: -f1 /etc/passwd > "$CURRENT_FILE"
if [ -f "$PREVIOUS_FILE" ]; then
  diff "$PREVIOUS_FILE" "$CURRENT_FILE" || echo "User account changes detected."
else
  echo "No previous user record found. Creating baseline."
fi
cp "$CURRENT_FILE" "$PREVIOUS_FILE"

Use Case: Run weekly to detect unauthorized changes in user accounts.

5. Auto System Update Script.

Purpose: Keep your Linux system up to date with minimal intervention.

#!/bin/bash
apt update && apt upgrade -y && apt autoremove -y
echo "System updated on $(date)" >> /var/log/sys_update.log

Use Case: Prevent unpatched vulnerabilities by automating updates.

6. Remote Server Health Ping.

Purpose: Check if a set of remote servers are online.

#!/bin/bash
SERVERS=("192.168.1.10" "192.168.1.11" "192.168.1.12")
for server in "${SERVERS[@]}"; do
  if ping -c 1 "$server" > /dev/null 2>&1; then
    echo "$server is reachable"
  else
    echo "$server is NOT reachable"
  fi
done

Use Case: Quickly verify network connectivity to essential infrastructure.

7. Disk Space Alert Script.

Purpose: Alert when a specific partition is nearing capacity.

#!/bin/bash
THRESHOLD=80
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "Warning: Disk usage is above ${THRESHOLD}% on root partition."
fi

Use Case: Integrate with monitoring systems or run via cron to prevent downtime due to full disks.

Best Practices for Using Bash Scripts in Admin Tasks.

  • Test scripts on non-production environments first
  • Use logging: Redirect outputs to log files for auditing
  • Set executable permissions: chmod +x script.sh
  • Use absolute paths to avoid unexpected failures
  • Add error handling where possible

Scheduling Scripts with Cron.

To automate these tasks, use crontab -e and add entries like:

0 1 * * * /usr/local/bin/backup.sh
0 8 * * 1 /usr/local/bin/healthcheck.sh

This example schedules a daily backup at 1 AM and a weekly health check every Monday at 8 AM.

Final Thoughts.

Bash scripts are more than just a convenience—they are an essential tool for modern system administrators who aim to work smarter, not harder. With just a bit of initial effort, you can automate daily tasks, reduce human error, and ensure your infrastructure runs like a well-oiled machine.

Investing time in writing robust shell scripts today can save you countless hours in the weeks, months, and years to come. Start small, build a library of trusted scripts, and scale your automation with confidence.

Leave a Reply

Your email address will not be published. Required fields are marked *