Automation Scripts
Automation is the heart of DevOps. Learn to create scripts that automate routine tasks, deployments, and system maintenance.
System Maintenance Script
Automate common maintenance tasks:
#!/bin/bash
set -euo pipefail
echo "=== System Maintenance ==="
# Update package lists
echo "Updating packages..."
apt-get update -qq
# Clean old packages
echo "Cleaning up..."
apt-get autoremove -y -qq
apt-get autoclean -qq
# Clear temp files
echo "Clearing temp files..."
find /tmp -type f -mtime +7 -delete 2>/dev/null || true
# Check disk usage
echo "Disk usage:"
df -h | grep -E '^/dev'
echo "Maintenance complete!"
File Organizer
Automatically organize files:
#!/bin/bash
SOURCE_DIR="${1:-.}"
TARGET_DIR="${2:-./organized}"
mkdir -p "$TARGET_DIR"/{images,documents,videos,audio,other}
for file in "$SOURCE_DIR"/*; do
[ -f "$file" ] || continue
case "${file,,}" in
*.jpg|*.jpeg|*.png|*.gif)
mv "$file" "$TARGET_DIR/images/" ;;
*.pdf|*.doc|*.docx|*.txt)
mv "$file" "$TARGET_DIR/documents/" ;;
*.mp4|*.avi|*.mkv)
mv "$file" "$TARGET_DIR/videos/" ;;
*.mp3|*.wav|*.flac)
mv "$file" "$TARGET_DIR/audio/" ;;
*)
mv "$file" "$TARGET_DIR/other/" ;;
esac
done
echo "Files organized!"
Exercise: File Cleanup
Automate cleanup:
Deployment Script
Basic application deployment:
#!/bin/bash
set -euo pipefail
APP_NAME="myapp"
DEPLOY_DIR="/var/www/$APP_NAME"
REPO_URL="git@github.com:user/$APP_NAME.git"
BRANCH="${1:-main}"
log() { echo "[$(date '+%H:%M:%S')] $*"; }
log "Starting deployment of $APP_NAME ($BRANCH)"
# Backup current version
if [ -d "$DEPLOY_DIR" ]; then
log "Backing up current version..."
tar -czf "/backup/${APP_NAME}_$(date +%Y%m%d_%H%M%S).tar.gz" "$DEPLOY_DIR"
fi
# Clone or pull
if [ -d "$DEPLOY_DIR/.git" ]; then
log "Pulling latest changes..."
cd "$DEPLOY_DIR"
git fetch origin
git checkout "$BRANCH"
git pull origin "$BRANCH"
else
log "Cloning repository..."
git clone -b "$BRANCH" "$REPO_URL" "$DEPLOY_DIR"
cd "$DEPLOY_DIR"
fi
# Install dependencies
log "Installing dependencies..."
npm install --production
# Restart service
log "Restarting service..."
systemctl restart "$APP_NAME"
log "Deployment complete!"
Scheduled Tasks
Create scripts for cron:
#!/bin/bash
# /etc/cron.daily/cleanup.sh
LOG="/var/log/cleanup.log"
{
echo "=== Cleanup started: $(date) ==="
# Remove old logs
find /var/log -name "*.log" -mtime +30 -delete
# Clear cache
rm -rf /var/cache/app/*
# Remove old sessions
find /var/lib/php/sessions -mtime +1 -delete
echo "=== Cleanup finished: $(date) ==="
} >> "$LOG" 2>&1
Health Check Script
Monitor system health:
#!/bin/bash
check_disk() {
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$usage" -gt 90 ]; then
echo "CRITICAL: Disk usage at ${usage}%"
return 1
fi
echo "OK: Disk usage at ${usage}%"
}
check_memory() {
free_mem=$(free | awk '/Mem:/ {printf "%.0f", $4/$2 * 100}')
if [ "$free_mem" -lt 10 ]; then
echo "CRITICAL: Only ${free_mem}% memory free"
return 1
fi
echo "OK: ${free_mem}% memory free"
}
check_service() {
local service="$1"
if systemctl is-active --quiet "$service"; then
echo "OK: $service is running"
else
echo "CRITICAL: $service is not running"
return 1
fi
}
# Run checks
STATUS=0
check_disk || STATUS=1
check_memory || STATUS=1
check_service nginx || STATUS=1
exit $STATUS
Exercise: Service Check
Check if a process is running:
Batch Processing
Process multiple items:
#!/bin/bash
# Process images in parallel
process_images() {
local src_dir="$1"
local dest_dir="$2"
mkdir -p "$dest_dir"
find "$src_dir" -name "*.jpg" | while read -r img; do
name=$(basename "$img")
echo "Processing: $name"
# Example: resize image
convert "$img" -resize 800x600 "$dest_dir/$name" &
done
wait # Wait for all background jobs
echo "All images processed"
}
process_images ./uploads ./thumbnails
CI/CD Helper Script
#!/bin/bash
set -euo pipefail
STAGE="${1:-test}"
run_tests() {
echo "Running tests..."
npm test
}
run_lint() {
echo "Running linter..."
npm run lint
}
build_app() {
echo "Building application..."
npm run build
}
deploy() {
local env="$1"
echo "Deploying to $env..."
# Deploy logic here
}
case "$STAGE" in
test)
run_tests
;;
lint)
run_lint
;;
build)
run_lint
run_tests
build_app
;;
deploy-staging)
build_app
deploy staging
;;
deploy-prod)
build_app
deploy production
;;
*)
echo "Usage: $0 {test|lint|build|deploy-staging|deploy-prod}"
exit 1
;;
esac
Automation Best Practices
- Idempotent scripts - Safe to run multiple times
- Logging - Record what happened and when
- Error handling - Fail gracefully, notify on errors
- Dry-run mode - Preview changes before applying
- Configuration - Use variables for customization
- Testing - Test scripts before production use
#!/bin/bash
# Best practices example
DRY_RUN="${DRY_RUN:-false}"
run() {
echo "Would run: $*"
[ "$DRY_RUN" = "true" ] && return
"$@"
}
# Usage
run rm -rf /tmp/old_files
# DRY_RUN=true ./script.sh # Preview mode
Key Takeaways
- Automate repetitive tasks to save time
- Use
set -euo pipefailfor safety - Add logging for troubleshooting
- Implement health checks for monitoring
- Use cron for scheduled automation
- Create idempotent scripts when possible
- Add dry-run modes for destructive operations
- Document your automation scripts

