Shell Scripting for Sysadmins: Automating Daily Tasks with Bash
Shows how to write simple Bash scripts to automate repetitive admin tasks like backups, log cleanup, and user creation, with real example…
Shell Scripting for Sysadmins: Automating Daily Tasks with Bash
Shows how to write simple Bash scripts to automate repetitive admin tasks like backups, log cleanup, and user creation, with real example scripts you can adapt.
🔧 Introduction: Why Sysadmins Need Bash
System administrators live in a world of repetition: backups, log rotations, user management, disk checks. Doing these tasks manually wastes time and introduces human error. Bash scripting is the sysadmin’s Swiss Army knife — lightweight, portable, and available on every Linux system.
With just a few lines of code, you can automate hours of work, enforce consistency, and sleep better knowing your servers are running predictable routines.

🧩 Fundamentals of Bash Scripting
Before diving into automation, let’s cover the essentials:
- Shebang (
#!/bin/bash) – Tells the system to run the script with Bash. - Variables — Store values like paths or usernames.
- Conditionals (
if/else) – Make decisions based on system state. - Loops (
for/while) – Repeat tasks across files or users. - Functions — Organize reusable code.
- Error Handling (
set -euo pipefail) – Prevent silent failures.
📂 Practical Scripts for Daily Admin Tasks
1. Automated Backups
bash
#!/bin/bash
set -euo pipefail
BACKUP_DIR="/var/backups"
SOURCE_DIR="/data"
DATE=$(date +%F)
tar -czf "$BACKUP_DIR/backup-$DATE.tar.gz" "$SOURCE_DIR"
echo "Backup completed: $BACKUP_DIR/backup-$DATE.tar.gz"
- Enhancement: Schedule with cron:
0 2 * * * /scripts/backup.sh.
2. Log Cleanup
bash
#!/bin/bash
LOG_DIR="/var/log"
find "$LOG_DIR" -type f -name "*.log" -mtime +7 -exec rm -f {} \;
echo "Old logs cleaned up."
- Use case: Remove logs older than 7 days.
- Benefit: Saves disk space, keeps logs manageable.
3. User Creation Script
bash
#!/bin/bash
USERS=("alice" "bob" "charlie")
for user in "${USERS[@]}"; do
if id "$user" &>/dev/null; then
echo "User $user already exists."
else
useradd -m "$user"
echo "User $user created."
fi
done
- Use case: Quickly onboard multiple users.
- Enhancement: Add password setup or group assignment.
4. Disk Usage Monitor
bash
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ "$USAGE" -gt "$THRESHOLD" ]; then
echo "Warning: Disk usage at $USAGE%!"
fi
- Use case: Alert when disk usage exceeds threshold.
- Enhancement: Send email notifications via
mail.
⚙️ Best Practices
- Always test scripts on non-production systems.
- Use logging (
tee,logger) to track script actions. - Secure scripts with proper permissions (
chmod 700). - Document scripts with comments for future admins.
- Cron-safe design — Ensure scripts don’t overlap or conflict.
🚀 When to Use Bash vs. Python
- Bash: Perfect for chaining commands, file operations, quick automation.
- Python: Better for complex data structures, APIs, JSON/YAML parsing. Rule of thumb: If it’s a sequence of shell commands, Bash wins. If it’s data-heavy, use Python.
메타데이터
- post_id
- fd836bfa3d3e
- slug
- shell-scripting-for-sysadmins-automating-daily-tasks-with-bash-fd836bfa3d3e
- url
- https://medium.com/@saketsis/shell-scripting-for-sysadmins-automating-daily-tasks-with-bash-fd836bfa3d3e
- canonical_url
- https://medium.com/@saketsis/shell-scripting-for-sysadmins-automating-daily-tasks-with-bash-fd836bfa3d3e
- author_url
- https://medium.com/@saketsis
- status
- ok
- fetched_at
- 2026-09-10 00:16:23