🤖 Configuring System Automation Scripts on Alpine Linux: Simple Guide
Automating tasks on Alpine Linux saves time and prevents mistakes! 💻 This guide shows you how to create and schedule automation scripts for system maintenance. Let’s automate everything! 😊
🤔 What is System Automation?
System automation means setting up scripts and schedules to run tasks automatically without manual intervention.
System automation is like:
- 📝 Robot assistants for your computer - Do repetitive tasks automatically
- 🔧 Scheduled maintenance workers - Keep your system running smoothly
- 💡 Smart timers for tasks - Execute commands at the right time
🎯 What You Need
Before we start, you need:
- ✅ Alpine Linux running on your computer
- ✅ Root access or sudo permissions
- ✅ Basic understanding of shell scripting
- ✅ Knowledge of system maintenance tasks
📋 Step 1: Install Automation Tools
Install Cron and Task Scheduler
Let’s install the tools for automating tasks! 😊
What we’re doing: Installing cron daemon and related automation utilities.
# Update package list
apk update
# Install cron daemon
apk add dcron
# Install additional automation tools
apk add logrotate rsync wget curl
# Install text processing tools for scripts
apk add grep sed awk findutils
# Start cron service
rc-service dcron start
rc-update add dcron boot
# Check cron service status
rc-service dcron status
# View cron daemon information
ps aux | grep cron
crontab -l 2>/dev/null || echo "No crontab configured yet"
What this does: 📖 Installs and configures the cron daemon for task scheduling.
Example output:
✅ (1/5) Installing dcron (4.5-r8)
✅ (2/5) Installing logrotate (3.20.1-r1)
✅ (3/5) Installing rsync (3.2.7-r0)
✅ (4/5) Installing wget (1.21.4-r0)
✅ (5/5) Installing findutils (4.9.0-r5)
✅ dcron * service started
✅ dcron added to boot runlevel
What this means: Your automation environment is ready! ✅
💡 Important Tips
Tip: Always test automation scripts manually first! 💡
Warning: Automated scripts can cause damage if misconfigured! ⚠️
🛠️ Step 2: Create Basic Automation Scripts
Build System Maintenance Scripts
Now let’s create essential system maintenance automation! 😊
What we’re doing: Creating scripts for common system maintenance tasks.
# Create automation scripts directory
mkdir -p ~/automation-scripts
cd ~/automation-scripts
# Create system cleanup script
cat > system_cleanup.sh << 'EOF'
#!/bin/bash
echo "🧹 System Cleanup Automation Script"
echo "Started: $(date)"
echo "=================================="
# Define log file
LOGFILE="/var/log/system_cleanup.log"
echo "$(date): Starting system cleanup" >> "$LOGFILE"
# Clean package cache
echo "📦 Cleaning package cache..."
apk cache clean
echo "$(date): Package cache cleaned" >> "$LOGFILE"
# Clean temporary files
echo "🗑️ Cleaning temporary files..."
find /tmp -type f -atime +7 -delete 2>/dev/null
find /var/tmp -type f -atime +7 -delete 2>/dev/null
echo "$(date): Temporary files cleaned" >> "$LOGFILE"
# Clean old log files
echo "📋 Rotating log files..."
logrotate -f /etc/logrotate.conf 2>/dev/null
echo "$(date): Log files rotated" >> "$LOGFILE"
# Update system information
echo "📊 Updating system information..."
updatedb 2>/dev/null || echo "locate database not available"
# Check disk usage
echo "💾 Checking disk usage..."
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 80 ]; then
echo "⚠️ WARNING: Disk usage is ${DISK_USAGE}%"
echo "$(date): HIGH DISK USAGE WARNING: ${DISK_USAGE}%" >> "$LOGFILE"
else
echo "✅ Disk usage is acceptable: ${DISK_USAGE}%"
fi
# Check memory usage
echo "🔧 Checking memory usage..."
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100.0}')
echo "Memory usage: ${MEMORY_USAGE}%"
# Clear swap if usage is high
SWAP_USAGE=$(free | grep Swap | awk '{if ($2 > 0) printf "%.0f", $3/$2 * 100.0; else print "0"}')
if [ "$SWAP_USAGE" -gt 50 ]; then
echo "🔄 Clearing swap cache..."
swapoff -a && swapon -a
echo "$(date): Swap cache cleared" >> "$LOGFILE"
fi
echo "=================================="
echo "Completed: $(date)"
echo "$(date): System cleanup completed" >> "$LOGFILE"
EOF
# Create backup automation script
cat > backup_automation.sh << 'EOF'
#!/bin/bash
echo "💾 Backup Automation Script"
echo "Started: $(date)"
echo "=========================="
# Configuration
BACKUP_SOURCE="/home /etc /var/www"
BACKUP_DEST="/backup"
BACKUP_NAME="system_backup_$(date +%Y%m%d_%H%M%S)"
RETENTION_DAYS=7
LOGFILE="/var/log/backup_automation.log"
echo "$(date): Starting backup automation" >> "$LOGFILE"
# Create backup destination
mkdir -p "$BACKUP_DEST"
# Create backup archive
echo "📁 Creating backup archive..."
tar -czf "$BACKUP_DEST/$BACKUP_NAME.tar.gz" $BACKUP_SOURCE 2>/dev/null
if [ $? -eq 0 ]; then
echo "✅ Backup created successfully: $BACKUP_NAME.tar.gz"
echo "$(date): Backup created: $BACKUP_NAME.tar.gz" >> "$LOGFILE"
# Check backup size
BACKUP_SIZE=$(du -h "$BACKUP_DEST/$BACKUP_NAME.tar.gz" | cut -f1)
echo "📊 Backup size: $BACKUP_SIZE"
echo "$(date): Backup size: $BACKUP_SIZE" >> "$LOGFILE"
else
echo "❌ Backup failed!"
echo "$(date): BACKUP FAILED" >> "$LOGFILE"
exit 1
fi
# Clean old backups
echo "🗑️ Cleaning old backups (older than $RETENTION_DAYS days)..."
find "$BACKUP_DEST" -name "system_backup_*.tar.gz" -mtime +$RETENTION_DAYS -delete
OLD_BACKUPS_REMOVED=$(find "$BACKUP_DEST" -name "system_backup_*.tar.gz" -mtime +$RETENTION_DAYS | wc -l)
echo "Removed $OLD_BACKUPS_REMOVED old backup(s)"
echo "$(date): Removed $OLD_BACKUPS_REMOVED old backups" >> "$LOGFILE"
# List current backups
echo ""
echo "📋 Current backups:"
ls -lah "$BACKUP_DEST"/system_backup_*.tar.gz 2>/dev/null | tail -5
echo "=========================="
echo "Completed: $(date)"
echo "$(date): Backup automation completed" >> "$LOGFILE"
EOF
# Create security monitoring script
cat > security_monitor.sh << 'EOF'
#!/bin/bash
echo "🔒 Security Monitoring Script"
echo "Started: $(date)"
echo "============================"
LOGFILE="/var/log/security_monitor.log"
ALERT_EMAIL="[email protected]" # Configure your email
echo "$(date): Starting security monitoring" >> "$LOGFILE"
# Check for failed login attempts
echo "🔍 Checking failed login attempts..."
FAILED_LOGINS=$(grep "authentication failure" /var/log/auth.log 2>/dev/null | wc -l)
if [ "$FAILED_LOGINS" -gt 10 ]; then
echo "⚠️ WARNING: $FAILED_LOGINS failed login attempts detected"
echo "$(date): HIGH FAILED LOGIN COUNT: $FAILED_LOGINS" >> "$LOGFILE"
else
echo "✅ Failed logins: $FAILED_LOGINS (acceptable)"
fi
# Check for unusual network connections
echo "🌐 Checking network connections..."
ACTIVE_CONNECTIONS=$(netstat -an | grep ESTABLISHED | wc -l)
echo "Active connections: $ACTIVE_CONNECTIONS"
# Check for running processes
echo "🔧 Checking running processes..."
PROCESS_COUNT=$(ps aux | wc -l)
echo "Running processes: $PROCESS_COUNT"
# Check system load
echo "📊 Checking system load..."
LOAD_AVERAGE=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
echo "Load average: $LOAD_AVERAGE"
# Check disk space usage
echo "💾 Checking disk space..."
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 90 ]; then
echo "🚨 CRITICAL: Disk usage is ${DISK_USAGE}%"
echo "$(date): CRITICAL DISK USAGE: ${DISK_USAGE}%" >> "$LOGFILE"
elif [ "$DISK_USAGE" -gt 80 ]; then
echo "⚠️ WARNING: Disk usage is ${DISK_USAGE}%"
echo "$(date): WARNING DISK USAGE: ${DISK_USAGE}%" >> "$LOGFILE"
else
echo "✅ Disk usage: ${DISK_USAGE}%"
fi
# Check for updates
echo "📦 Checking for system updates..."
UPDATE_COUNT=$(apk list --upgradable 2>/dev/null | wc -l)
if [ "$UPDATE_COUNT" -gt 10 ]; then
echo "📱 $UPDATE_COUNT updates available"
echo "$(date): $UPDATE_COUNT updates available" >> "$LOGFILE"
else
echo "✅ System is up to date ($UPDATE_COUNT updates)"
fi
echo "============================"
echo "Completed: $(date)"
echo "$(date): Security monitoring completed" >> "$LOGFILE"
EOF
# Make scripts executable
chmod +x *.sh
echo "✅ Automation scripts created successfully!"
ls -la *.sh
Code explanation:
system_cleanup.sh
: Automates system cleaning and maintenancebackup_automation.sh
: Creates and manages system backupssecurity_monitor.sh
: Monitors system security and alertschmod +x
: Makes scripts executable
Expected Output:
✅ Automation scripts created successfully!
-rwxr-xr-x 1 user user 2847 system_cleanup.sh
-rwxr-xr-x 1 user user 2156 backup_automation.sh
-rwxr-xr-x 1 user user 1923 security_monitor.sh
What this means: Great job! Your automation scripts are ready! 🎉
🎮 Let’s Test Automation Scripts!
Time for hands-on practice! This is the fun part! 🎯
What we’re doing: Testing automation scripts before scheduling them.
# Test system cleanup script
echo "🧪 Testing system cleanup script:"
cd ~/automation-scripts
./system_cleanup.sh
# Test backup script (create test directory first)
echo ""
echo "🧪 Testing backup script:"
mkdir -p /tmp/test_backup_source
echo "Test file for backup" > /tmp/test_backup_source/test.txt
# Temporarily modify backup script for testing
sed 's|BACKUP_SOURCE="/home /etc /var/www"|BACKUP_SOURCE="/tmp/test_backup_source"|' backup_automation.sh > test_backup.sh
sed -i 's|BACKUP_DEST="/backup"|BACKUP_DEST="/tmp/test_backup"|' test_backup.sh
chmod +x test_backup.sh
./test_backup.sh
# Test security monitoring
echo ""
echo "🧪 Testing security monitoring script:"
./security_monitor.sh
# Clean up test files
rm -rf /tmp/test_backup /tmp/test_backup_source test_backup.sh
echo ""
echo "📊 Script Test Results:"
echo "✅ System cleanup script executed"
echo "✅ Backup automation script executed"
echo "✅ Security monitoring script executed"
You should see:
🧪 Testing system cleanup script:
🧹 System Cleanup Automation Script
📦 Cleaning package cache...
🗑️ Cleaning temporary files...
✅ Disk usage is acceptable: 45%
🧪 Testing security monitoring script:
🔒 Security Monitoring Script
🔍 Checking failed login attempts...
✅ Failed logins: 0 (acceptable)
📊 Script Test Results:
✅ All scripts executed successfully
Awesome work! 🌟
📊 Automation Schedule Types
Schedule Type | Cron Expression | Description | Example Use Case |
---|---|---|---|
🔧 Every Hour | 0 * * * * | Runs at minute 0 of every hour | System monitoring |
🛠️ Daily | 0 2 * * * | Runs at 2:00 AM every day | System cleanup |
🎯 Weekly | 0 3 * * 0 | Runs at 3:00 AM every Sunday | Full backup |
💾 Monthly | 0 4 1 * * | Runs at 4:00 AM on 1st of month | Archive cleanup |
🛠️ Step 3: Schedule Automation with Cron
Configure Cron Jobs
What we’re doing: Setting up automatic scheduling for our automation scripts.
# Create cron jobs for automation scripts
echo "⏰ Configuring cron jobs for automation..."
# Backup current crontab (if exists)
crontab -l > ~/crontab_backup.txt 2>/dev/null || echo "# No existing crontab" > ~/crontab_backup.txt
# Create new crontab configuration
cat > ~/automation_crontab.txt << 'EOF'
# System Automation Cron Jobs
# Format: minute hour day_of_month month day_of_week command
# Daily system cleanup at 2:00 AM
0 2 * * * /home/$(whoami)/automation-scripts/system_cleanup.sh
# Weekly backup every Sunday at 3:00 AM
0 3 * * 0 /home/$(whoami)/automation-scripts/backup_automation.sh
# Security monitoring every 4 hours
0 */4 * * * /home/$(whoami)/automation-scripts/security_monitor.sh
# Update package list daily at 1:00 AM
0 1 * * * /sbin/apk update
# Check disk space every 2 hours and log results
0 */2 * * * df -h >> /var/log/disk_usage.log
# Restart services if needed (weekly maintenance)
30 3 * * 0 /sbin/rc-service sshd restart && /sbin/rc-service networking restart
EOF
# Install crontab
crontab ~/automation_crontab.txt
# Verify crontab installation
echo "📋 Current crontab configuration:"
crontab -l
# Check cron service logs
echo ""
echo "📊 Cron service status:"
rc-service dcron status
# Test cron job syntax
echo ""
echo "🧪 Testing cron job syntax:"
for line in $(crontab -l | grep -v '^#' | grep -v '^$'); do
echo "✅ Valid cron entry: $line"
done
What this does: Sets up automatic scheduling for all automation tasks! 🌟
Create Advanced Cron Management
What we’re doing: Building tools to manage and monitor cron jobs effectively.
# Create cron management script
cat > ~/bin/cron_manager.sh << 'EOF'
#!/bin/bash
echo "⏰ Cron Job Manager"
echo "=================="
show_cron_status() {
echo "📊 Cron Service Status:"
rc-service dcron status
echo ""
echo "📋 Current Cron Jobs:"
crontab -l 2>/dev/null || echo "No cron jobs configured"
echo ""
echo "🔍 Recent Cron Activity:"
tail -10 /var/log/cron.log 2>/dev/null || echo "No cron logs available"
}
add_cron_job() {
echo "➕ Adding New Cron Job"
echo "====================="
read -p "Enter cron schedule (e.g., '0 2 * * *'): " schedule
read -p "Enter command to execute: " command
read -p "Enter description: " description
# Validate cron schedule format
if ! echo "$schedule" | grep -E '^[0-9*,/-]+ [0-9*,/-]+ [0-9*,/-]+ [0-9*,/-]+ [0-9*,/-]+$' >/dev/null; then
echo "❌ Invalid cron schedule format"
return 1
fi
# Get current crontab
crontab -l > /tmp/current_crontab 2>/dev/null || echo "" > /tmp/current_crontab
# Add new job
echo "# $description" >> /tmp/current_crontab
echo "$schedule $command" >> /tmp/current_crontab
# Install updated crontab
crontab /tmp/current_crontab
echo "✅ Cron job added successfully!"
rm /tmp/current_crontab
}
remove_cron_job() {
echo "➖ Removing Cron Job"
echo "==================="
echo "Current cron jobs:"
crontab -l 2>/dev/null | grep -n "." || echo "No cron jobs found"
read -p "Enter line number to remove: " line_num
if ! [[ "$line_num" =~ ^[0-9]+$ ]]; then
echo "❌ Invalid line number"
return 1
fi
# Get current crontab and remove specified line
crontab -l > /tmp/current_crontab 2>/dev/null
sed "${line_num}d" /tmp/current_crontab > /tmp/updated_crontab
crontab /tmp/updated_crontab
echo "✅ Cron job removed successfully!"
rm /tmp/current_crontab /tmp/updated_crontab
}
test_cron_job() {
echo "🧪 Testing Cron Job"
echo "=================="
read -p "Enter command to test: " command
echo "Executing: $command"
eval "$command"
if [ $? -eq 0 ]; then
echo "✅ Command executed successfully"
else
echo "❌ Command failed"
fi
}
backup_crontab() {
echo "💾 Backing Up Crontab"
echo "===================="
backup_file="$HOME/crontab_backup_$(date +%Y%m%d_%H%M%S).txt"
crontab -l > "$backup_file" 2>/dev/null
if [ $? -eq 0 ]; then
echo "✅ Crontab backed up to: $backup_file"
else
echo "❌ No crontab to backup"
fi
}
restore_crontab() {
echo "🔄 Restoring Crontab"
echo "==================="
echo "Available backup files:"
ls -la ~/crontab_backup_*.txt 2>/dev/null || echo "No backup files found"
read -p "Enter backup file path: " backup_file
if [ -f "$backup_file" ]; then
crontab "$backup_file"
echo "✅ Crontab restored from: $backup_file"
else
echo "❌ Backup file not found: $backup_file"
fi
}
show_menu() {
echo ""
echo "Choose an action:"
echo "1. Show cron status and jobs"
echo "2. Add new cron job"
echo "3. Remove cron job"
echo "4. Test command"
echo "5. Backup crontab"
echo "6. Restore crontab"
echo "0. Exit"
echo ""
}
while true; do
show_menu
read -p "Enter your choice [0-6]: " choice
case $choice in
1) show_cron_status ;;
2) add_cron_job ;;
3) remove_cron_job ;;
4) test_cron_job ;;
5) backup_crontab ;;
6) restore_crontab ;;
0) echo "👋 Goodbye!"; exit 0 ;;
*) echo "❌ Invalid option. Please try again." ;;
esac
read -p "Press Enter to continue..."
done
echo "=================="
EOF
# Make executable
mkdir -p ~/bin
chmod +x ~/bin/cron_manager.sh
echo "✅ Cron management tool created!"
echo "📱 Run: ~/bin/cron_manager.sh"
Expected Output:
✅ Cron jobs configured successfully
📋 Current crontab configuration:
# Daily system cleanup at 2:00 AM
0 2 * * * /home/user/automation-scripts/system_cleanup.sh
# Weekly backup every Sunday at 3:00 AM
0 3 * * 0 /home/user/automation-scripts/backup_automation.sh
✅ Cron management tool created!
What this does: Provides comprehensive cron job management capabilities! 💫
🛠️ Step 4: Advanced Automation Features
Implement Conditional Automation
What we’re doing: Creating smart automation that adapts to system conditions.
# Create intelligent automation script
cat > ~/automation-scripts/smart_automation.sh << 'EOF'
#!/bin/bash
echo "🤖 Smart Automation Engine"
echo "Started: $(date)"
echo "=========================="
LOGFILE="/var/log/smart_automation.log"
CONFIG_FILE="$HOME/automation-scripts/automation_config.conf"
# Load configuration
load_config() {
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
else
# Create default configuration
cat > "$CONFIG_FILE" << 'CONFIG_EOF'
# Smart Automation Configuration
# Thresholds
DISK_WARNING_THRESHOLD=80
DISK_CRITICAL_THRESHOLD=90
MEMORY_WARNING_THRESHOLD=80
LOAD_WARNING_THRESHOLD=2.0
# Automation settings
AUTO_CLEANUP_ENABLED=true
AUTO_BACKUP_ENABLED=true
AUTO_UPDATE_ENABLED=false
AUTO_RESTART_ENABLED=false
# Notification settings
EMAIL_NOTIFICATIONS=false
LOG_NOTIFICATIONS=true
CONFIG_EOF
echo "📝 Created default configuration: $CONFIG_FILE"
source "$CONFIG_FILE"
fi
}
# Check system conditions
check_system_health() {
echo "🔍 Checking system health..."
# Check disk usage
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
echo "Disk usage: ${DISK_USAGE}%"
# Check memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100.0}')
echo "Memory usage: ${MEMORY_USAGE}%"
# Check load average
LOAD_AVERAGE=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
echo "Load average: $LOAD_AVERAGE"
# Check running processes
PROCESS_COUNT=$(ps aux | wc -l)
echo "Running processes: $PROCESS_COUNT"
}
# Conditional cleanup
conditional_cleanup() {
if [ "$AUTO_CLEANUP_ENABLED" = "true" ]; then
if [ "$DISK_USAGE" -gt "$DISK_WARNING_THRESHOLD" ]; then
echo "💾 Disk usage above threshold, starting cleanup..."
# Clean package cache
apk cache clean
# Clean temporary files
find /tmp -type f -atime +1 -delete 2>/dev/null
# Clean old logs
find /var/log -name "*.log" -mtime +7 -exec gzip {} \; 2>/dev/null
echo "$(date): Conditional cleanup performed - disk usage was ${DISK_USAGE}%" >> "$LOGFILE"
# Recheck disk usage
NEW_DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
echo "✅ Cleanup complete. New disk usage: ${NEW_DISK_USAGE}%"
else
echo "✅ Disk usage acceptable, no cleanup needed"
fi
fi
}
# Conditional backup
conditional_backup() {
if [ "$AUTO_BACKUP_ENABLED" = "true" ]; then
# Only backup if it's been more than 24 hours since last backup
LAST_BACKUP=$(find /backup -name "system_backup_*.tar.gz" -mtime -1 2>/dev/null | wc -l)
if [ "$LAST_BACKUP" -eq 0 ]; then
echo "💾 No recent backup found, creating backup..."
~/automation-scripts/backup_automation.sh
echo "$(date): Conditional backup performed" >> "$LOGFILE"
else
echo "✅ Recent backup exists, skipping backup"
fi
fi
}
# Conditional system updates
conditional_updates() {
if [ "$AUTO_UPDATE_ENABLED" = "true" ]; then
# Check for updates
UPDATE_COUNT=$(apk list --upgradable 2>/dev/null | wc -l)
if [ "$UPDATE_COUNT" -gt 0 ]; then
echo "📦 $UPDATE_COUNT updates available, applying updates..."
apk upgrade
echo "$(date): Applied $UPDATE_COUNT system updates" >> "$LOGFILE"
else
echo "✅ System is up to date"
fi
fi
}
# Service health check and restart
service_health_check() {
if [ "$AUTO_RESTART_ENABLED" = "true" ]; then
echo "🔧 Checking service health..."
# Check SSH service
if ! rc-service sshd status >/dev/null 2>&1; then
echo "⚠️ SSH service not running, restarting..."
rc-service sshd restart
echo "$(date): SSH service restarted" >> "$LOGFILE"
fi
# Check cron service
if ! rc-service dcron status >/dev/null 2>&1; then
echo "⚠️ Cron service not running, restarting..."
rc-service dcron restart
echo "$(date): Cron service restarted" >> "$LOGFILE"
fi
echo "✅ Service health check complete"
fi
}
# Generate automation report
generate_report() {
REPORT_FILE="/var/log/automation_report_$(date +%Y%m%d).log"
echo "📊 Automation Report - $(date)" > "$REPORT_FILE"
echo "=======================================" >> "$REPORT_FILE"
echo "Disk Usage: ${DISK_USAGE}%" >> "$REPORT_FILE"
echo "Memory Usage: ${MEMORY_USAGE}%" >> "$REPORT_FILE"
echo "Load Average: $LOAD_AVERAGE" >> "$REPORT_FILE"
echo "Process Count: $PROCESS_COUNT" >> "$REPORT_FILE"
echo "=======================================" >> "$REPORT_FILE"
echo "📋 Report generated: $REPORT_FILE"
}
# Main execution flow
load_config
check_system_health
conditional_cleanup
conditional_backup
conditional_updates
service_health_check
generate_report
echo "=========================="
echo "Completed: $(date)"
echo "$(date): Smart automation cycle completed" >> "$LOGFILE"
EOF
# Make executable
chmod +x ~/automation-scripts/smart_automation.sh
# Test smart automation
echo "🧪 Testing smart automation..."
~/automation-scripts/smart_automation.sh
echo "✅ Smart automation system ready!"
What this does: Creates intelligent automation that adapts to system conditions! 💫
Create Automation Dashboard
What we’re doing: Building a monitoring dashboard for automation tasks.
# Create automation dashboard script
cat > ~/bin/automation_dashboard.sh << 'EOF'
#!/bin/bash
echo "📊 System Automation Dashboard"
echo "============================="
# Function to get automation status
get_automation_status() {
echo "🤖 Automation Status Overview"
echo "----------------------------"
# Cron service status
if rc-service dcron status >/dev/null 2>&1; then
echo "✅ Cron Service: Running"
else
echo "❌ Cron Service: Stopped"
fi
# Count active cron jobs
CRON_JOBS=$(crontab -l 2>/dev/null | grep -v '^#' | grep -v '^$' | wc -l)
echo "📋 Active Cron Jobs: $CRON_JOBS"
# Last automation run
if [ -f "/var/log/smart_automation.log" ]; then
LAST_RUN=$(tail -1 /var/log/smart_automation.log | cut -d: -f1-3)
echo "⏰ Last Automation Run: $LAST_RUN"
else
echo "⏰ Last Automation Run: Never"
fi
echo ""
}
# Function to show recent automation activity
show_recent_activity() {
echo "📋 Recent Automation Activity"
echo "----------------------------"
# Show recent cron job executions
if [ -f "/var/log/cron.log" ]; then
echo "🔄 Recent Cron Executions:"
tail -5 /var/log/cron.log 2>/dev/null | while read line; do
echo " $line"
done
else
echo "No cron log available"
fi
echo ""
# Show recent automation logs
if [ -f "/var/log/smart_automation.log" ]; then
echo "🤖 Recent Smart Automation:"
tail -5 /var/log/smart_automation.log | while read line; do
echo " $line"
done
else
echo "No smart automation logs available"
fi
echo ""
}
# Function to show system health metrics
show_system_metrics() {
echo "📊 Current System Metrics"
echo "------------------------"
# Disk usage
DISK_USAGE=$(df / | tail -1 | awk '{print $5}')
echo "💾 Disk Usage: $DISK_USAGE"
# Memory usage
MEMORY_INFO=$(free -h | grep Mem | awk '{print $3 "/" $2 " (" int($3/$2*100) "%)"}')
echo "🔧 Memory Usage: $MEMORY_INFO"
# Load average
LOAD_AVERAGE=$(uptime | awk -F'load average:' '{print $2}')
echo "⚡ Load Average:$LOAD_AVERAGE"
# Uptime
UPTIME=$(uptime | awk -F'up ' '{print $2}' | awk -F',' '{print $1}')
echo "⏰ System Uptime: $UPTIME"
echo ""
}
# Function to show backup status
show_backup_status() {
echo "💾 Backup Status"
echo "---------------"
if [ -d "/backup" ]; then
BACKUP_COUNT=$(ls /backup/system_backup_*.tar.gz 2>/dev/null | wc -l)
echo "📦 Total Backups: $BACKUP_COUNT"
if [ "$BACKUP_COUNT" -gt 0 ]; then
LATEST_BACKUP=$(ls -t /backup/system_backup_*.tar.gz 2>/dev/null | head -1)
LATEST_DATE=$(stat -c %y "$LATEST_BACKUP" 2>/dev/null | cut -d' ' -f1,2 | cut -d'.' -f1)
BACKUP_SIZE=$(du -h "$LATEST_BACKUP" 2>/dev/null | cut -f1)
echo "📅 Latest Backup: $LATEST_DATE"
echo "📊 Latest Backup Size: $BACKUP_SIZE"
fi
else
echo "❌ No backup directory found"
fi
echo ""
}
# Function to show automation configuration
show_automation_config() {
echo "⚙️ Automation Configuration"
echo "---------------------------"
CONFIG_FILE="$HOME/automation-scripts/automation_config.conf"
if [ -f "$CONFIG_FILE" ]; then
echo "📝 Configuration loaded from: $CONFIG_FILE"
echo ""
grep -E '^[A-Z_]+=.*' "$CONFIG_FILE" | while read line; do
echo " $line"
done
else
echo "❌ No configuration file found"
fi
echo ""
}
# Function to check for automation issues
check_automation_issues() {
echo "🔍 Automation Health Check"
echo "-------------------------"
ISSUES=0
# Check if cron is running
if ! rc-service dcron status >/dev/null 2>&1; then
echo "❌ Cron service is not running"
ISSUES=$((ISSUES + 1))
fi
# Check if automation scripts exist
if [ ! -f "$HOME/automation-scripts/system_cleanup.sh" ]; then
echo "❌ System cleanup script missing"
ISSUES=$((ISSUES + 1))
fi
if [ ! -f "$HOME/automation-scripts/backup_automation.sh" ]; then
echo "❌ Backup automation script missing"
ISSUES=$((ISSUES + 1))
fi
# Check disk space
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 90 ]; then
echo "❌ Critical disk usage: ${DISK_USAGE}%"
ISSUES=$((ISSUES + 1))
elif [ "$DISK_USAGE" -gt 80 ]; then
echo "⚠️ High disk usage: ${DISK_USAGE}%"
fi
# Check for old logs
OLD_LOGS=$(find /var/log -name "*.log" -mtime +30 2>/dev/null | wc -l)
if [ "$OLD_LOGS" -gt 10 ]; then
echo "⚠️ $OLD_LOGS old log files found (consider cleanup)"
fi
if [ "$ISSUES" -eq 0 ]; then
echo "✅ No automation issues detected"
else
echo "❌ Found $ISSUES automation issue(s)"
fi
echo ""
}
# Main dashboard display
clear
get_automation_status
show_system_metrics
show_backup_status
show_recent_activity
show_automation_config
check_automation_issues
echo "============================="
echo "Dashboard updated: $(date)"
echo "Press Ctrl+C to exit, or run again to refresh"
EOF
# Make executable
chmod +x ~/bin/automation_dashboard.sh
# Test dashboard
~/bin/automation_dashboard.sh
echo "✅ Automation dashboard created!"
echo "📱 Run: ~/bin/automation_dashboard.sh"
Expected Output:
📊 System Automation Dashboard
=============================
🤖 Automation Status Overview
✅ Cron Service: Running
📋 Active Cron Jobs: 6
⏰ Last Automation Run: Jun 18 02:15:33
📊 Current System Metrics
💾 Disk Usage: 45%
🔧 Memory Usage: 1.2G/4.0G (30%)
✅ Automation dashboard created!
What this does: Provides comprehensive automation monitoring and control! 📚
🎮 Practice Time!
Let’s practice what you learned! Try these simple examples:
Example 1: Custom Automation Workflow 🟢
What we’re doing: Creating a custom automation workflow for specific tasks.
# Create custom workflow automation
mkdir -p ~/automation-scripts/workflows
cd ~/automation-scripts/workflows
# Create development environment automation
cat > dev_environment_automation.sh << 'EOF'
#!/bin/bash
echo "💻 Development Environment Automation"
echo "Started: $(date)"
echo "===================================="
LOGFILE="/var/log/dev_automation.log"
PROJECT_DIR="$HOME/projects"
echo "$(date): Starting development environment automation" >> "$LOGFILE"
# Update development tools
echo "📦 Updating development tools..."
apk update
apk upgrade nodejs npm python3 py3-pip git
# Clean up development workspace
echo "🧹 Cleaning development workspace..."
if [ -d "$PROJECT_DIR" ]; then
# Remove node_modules directories older than 7 days
find "$PROJECT_DIR" -name "node_modules" -type d -mtime +7 -exec rm -rf {} + 2>/dev/null
# Clean Python cache
find "$PROJECT_DIR" -name "__pycache__" -type d -exec rm -rf {} + 2>/dev/null
find "$PROJECT_DIR" -name "*.pyc" -delete 2>/dev/null
# Clean build artifacts
find "$PROJECT_DIR" -name "dist" -type d -mtime +7 -exec rm -rf {} + 2>/dev/null
find "$PROJECT_DIR" -name "build" -type d -mtime +7 -exec rm -rf {} + 2>/dev/null
fi
# Update Git repositories
echo "📡 Updating Git repositories..."
if [ -d "$PROJECT_DIR" ]; then
find "$PROJECT_DIR" -name ".git" -type d | while read git_dir; do
project_dir=$(dirname "$git_dir")
echo " Updating: $project_dir"
cd "$project_dir"
git fetch --all 2>/dev/null || echo " Failed to fetch updates"
cd - >/dev/null
done
fi
# Check development environment health
echo "🔍 Checking development environment..."
echo " Node.js: $(node --version 2>/dev/null || echo 'Not installed')"
echo " npm: $(npm --version 2>/dev/null || echo 'Not installed')"
echo " Python: $(python3 --version 2>/dev/null || echo 'Not installed')"
echo " Git: $(git --version 2>/dev/null || echo 'Not installed')"
# Generate development report
DEV_REPORT="/var/log/dev_environment_report_$(date +%Y%m%d).log"
echo "Development Environment Report - $(date)" > "$DEV_REPORT"
echo "=========================================" >> "$DEV_REPORT"
echo "Project directories: $(find $PROJECT_DIR -maxdepth 1 -type d 2>/dev/null | wc -l)" >> "$DEV_REPORT"
echo "Git repositories: $(find $PROJECT_DIR -name '.git' -type d 2>/dev/null | wc -l)" >> "$DEV_REPORT"
echo "Disk usage in projects: $(du -sh $PROJECT_DIR 2>/dev/null | cut -f1)" >> "$DEV_REPORT"
echo "===================================="
echo "Completed: $(date)"
echo "$(date): Development environment automation completed" >> "$LOGFILE"
EOF
# Create web server maintenance automation
cat > webserver_maintenance.sh << 'EOF'
#!/bin/bash
echo "🌐 Web Server Maintenance Automation"
echo "Started: $(date)"
echo "===================================="
LOGFILE="/var/log/webserver_automation.log"
WEB_ROOT="/var/www/html"
echo "$(date): Starting web server maintenance" >> "$LOGFILE"
# Check web server status
echo "🔍 Checking web server status..."
if command -v nginx >/dev/null 2>&1; then
if rc-service nginx status >/dev/null 2>&1; then
echo "✅ Nginx is running"
else
echo "⚠️ Nginx is not running, attempting to start..."
rc-service nginx start
echo "$(date): Nginx service restarted" >> "$LOGFILE"
fi
else
echo "ℹ️ Nginx not installed"
fi
# Clean web server logs
echo "📋 Managing web server logs..."
if [ -d "/var/log/nginx" ]; then
# Compress old access logs
find /var/log/nginx -name "access.log.*" -mtime +7 -exec gzip {} \;
# Remove very old compressed logs
find /var/log/nginx -name "*.gz" -mtime +30 -delete
echo "$(date): Web server logs cleaned" >> "$LOGFILE"
fi
# Check SSL certificates
echo "🔒 Checking SSL certificates..."
if [ -d "/etc/ssl/certs" ]; then
EXPIRED_CERTS=$(find /etc/ssl/certs -name "*.crt" -exec openssl x509 -in {} -noout -checkend 2592000 \; 2>/dev/null | grep -c "will expire")
if [ "$EXPIRED_CERTS" -gt 0 ]; then
echo "⚠️ $EXPIRED_CERTS SSL certificates will expire within 30 days"
echo "$(date): WARNING: $EXPIRED_CERTS SSL certificates expiring soon" >> "$LOGFILE"
else
echo "✅ SSL certificates are valid"
fi
fi
# Check website accessibility
echo "🌍 Testing website accessibility..."
if command -v curl >/dev/null 2>&1; then
if curl -s -o /dev/null -w "%{http_code}" http://localhost | grep -q "200"; then
echo "✅ Website is accessible"
else
echo "❌ Website is not accessible"
echo "$(date): WARNING: Website not accessible" >> "$LOGFILE"
fi
fi
# Clean temporary web files
echo "🧹 Cleaning temporary web files..."
if [ -d "$WEB_ROOT" ]; then
find "$WEB_ROOT" -name "*.tmp" -mtime +1 -delete 2>/dev/null
find "$WEB_ROOT" -name ".DS_Store" -delete 2>/dev/null
fi
echo "===================================="
echo "Completed: $(date)"
echo "$(date): Web server maintenance completed" >> "$LOGFILE"
EOF
# Make scripts executable
chmod +x *.sh
echo "✅ Custom automation workflows created!"
ls -la *.sh
What this does: Creates specialized automation workflows for different scenarios! 🌟
Example 2: Automation Monitoring and Alerting 🟡
What we’re doing: Building comprehensive monitoring and alerting for automation systems.
# Create automation monitoring system
cat > ~/bin/automation_monitor.sh << 'EOF'
#!/bin/bash
echo "🔔 Automation Monitoring and Alerting System"
echo "============================================"
MONITOR_CONFIG="$HOME/.automation_monitor.conf"
ALERT_LOG="/var/log/automation_alerts.log"
# Load monitoring configuration
load_monitor_config() {
if [ ! -f "$MONITOR_CONFIG" ]; then
cat > "$MONITOR_CONFIG" << 'CONFIG_EOF'
# Automation Monitoring Configuration
# Alert thresholds
DISK_ALERT_THRESHOLD=85
MEMORY_ALERT_THRESHOLD=90
LOAD_ALERT_THRESHOLD=3.0
FAILED_JOBS_THRESHOLD=3
# Monitoring intervals (in minutes)
CRON_CHECK_INTERVAL=60
SYSTEM_CHECK_INTERVAL=30
LOG_CHECK_INTERVAL=15
# Notification settings
EMAIL_ALERTS=false
LOG_ALERTS=true
CONSOLE_ALERTS=true
# Email configuration (if enabled)
ALERT_EMAIL="[email protected]"
SMTP_SERVER="localhost"
CONFIG_EOF
fi
source "$MONITOR_CONFIG"
}
# Check automation job status
check_automation_jobs() {
echo "🔍 Checking automation job status..."
# Check if cron is running
if ! rc-service dcron status >/dev/null 2>&1; then
send_alert "CRITICAL" "Cron service is not running"
return 1
fi
# Check for recent cron job failures
if [ -f "/var/log/cron.log" ]; then
FAILED_JOBS=$(grep "$(date +%Y-%m-%d)" /var/log/cron.log | grep -c "FAILED\|ERROR" 2>/dev/null)
if [ "$FAILED_JOBS" -gt "$FAILED_JOBS_THRESHOLD" ]; then
send_alert "WARNING" "$FAILED_JOBS automation jobs failed today"
fi
fi
# Check if automation scripts are executable
SCRIPT_DIR="$HOME/automation-scripts"
if [ -d "$SCRIPT_DIR" ]; then
NON_EXECUTABLE=$(find "$SCRIPT_DIR" -name "*.sh" ! -executable | wc -l)
if [ "$NON_EXECUTABLE" -gt 0 ]; then
send_alert "WARNING" "$NON_EXECUTABLE automation scripts are not executable"
fi
fi
echo "✅ Automation job check completed"
}
# Check system resources
check_system_resources() {
echo "📊 Checking system resources..."
# Check disk usage
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt "$DISK_ALERT_THRESHOLD" ]; then
send_alert "WARNING" "High disk usage: ${DISK_USAGE}%"
fi
# Check memory usage
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100.0}')
if [ "$MEMORY_USAGE" -gt "$MEMORY_ALERT_THRESHOLD" ]; then
send_alert "WARNING" "High memory usage: ${MEMORY_USAGE}%"
fi
# Check load average
LOAD_AVERAGE=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
LOAD_INT=$(echo "$LOAD_AVERAGE" | cut -d'.' -f1)
if [ "$LOAD_INT" -gt "$(echo $LOAD_ALERT_THRESHOLD | cut -d'.' -f1)" ]; then
send_alert "WARNING" "High system load: $LOAD_AVERAGE"
fi
echo "✅ System resource check completed"
}
# Check automation logs for errors
check_automation_logs() {
echo "📋 Checking automation logs..."
# Check for recent errors in automation logs
LOG_DIR="/var/log"
TODAY=$(date +%Y-%m-%d)
# Check system automation log
if [ -f "$LOG_DIR/smart_automation.log" ]; then
ERROR_COUNT=$(grep "$TODAY" "$LOG_DIR/smart_automation.log" | grep -ic "error\|failed\|critical")
if [ "$ERROR_COUNT" -gt 0 ]; then
send_alert "WARNING" "$ERROR_COUNT errors found in smart automation log"
fi
fi
# Check backup automation log
if [ -f "$LOG_DIR/backup_automation.log" ]; then
BACKUP_FAILURES=$(grep "$TODAY" "$LOG_DIR/backup_automation.log" | grep -ic "backup failed")
if [ "$BACKUP_FAILURES" -gt 0 ]; then
send_alert "CRITICAL" "Backup automation failed $BACKUP_FAILURES times today"
fi
fi
echo "✅ Automation log check completed"
}
# Send alert function
send_alert() {
local severity="$1"
local message="$2"
local timestamp=$(date)
local alert_entry="[$timestamp] $severity: $message"
# Log alert
if [ "$LOG_ALERTS" = "true" ]; then
echo "$alert_entry" >> "$ALERT_LOG"
fi
# Console alert
if [ "$CONSOLE_ALERTS" = "true" ]; then
case "$severity" in
"CRITICAL") echo "🚨 $alert_entry" ;;
"WARNING") echo "⚠️ $alert_entry" ;;
"INFO") echo "ℹ️ $alert_entry" ;;
esac
fi
# Email alert (if configured)
if [ "$EMAIL_ALERTS" = "true" ] && [ ! -z "$ALERT_EMAIL" ]; then
echo "$alert_entry" | mail -s "Automation Alert: $severity" "$ALERT_EMAIL" 2>/dev/null
fi
}
# Generate monitoring report
generate_monitoring_report() {
REPORT_FILE="/var/log/automation_monitoring_$(date +%Y%m%d_%H%M%S).log"
echo "Automation Monitoring Report" > "$REPORT_FILE"
echo "Generated: $(date)" >> "$REPORT_FILE"
echo "==============================" >> "$REPORT_FILE"
# System status
echo "" >> "$REPORT_FILE"
echo "System Status:" >> "$REPORT_FILE"
echo " Disk Usage: $(df / | tail -1 | awk '{print $5}')" >> "$REPORT_FILE"
echo " Memory Usage: $(free | grep Mem | awk '{printf "%.0f%%", $3/$2 * 100.0}')" >> "$REPORT_FILE"
echo " Load Average: $(uptime | awk -F'load average:' '{print $2}')" >> "$REPORT_FILE"
# Automation status
echo "" >> "$REPORT_FILE"
echo "Automation Status:" >> "$REPORT_FILE"
echo " Cron Service: $(rc-service dcron status >/dev/null 2>&1 && echo 'Running' || echo 'Stopped')" >> "$REPORT_FILE"
echo " Active Jobs: $(crontab -l 2>/dev/null | grep -v '^#' | grep -v '^$' | wc -l)" >> "$REPORT_FILE"
# Recent alerts
echo "" >> "$REPORT_FILE"
echo "Recent Alerts (last 24 hours):" >> "$REPORT_FILE"
if [ -f "$ALERT_LOG" ]; then
grep "$(date +%Y-%m-%d)" "$ALERT_LOG" >> "$REPORT_FILE" 2>/dev/null || echo " No alerts in last 24 hours" >> "$REPORT_FILE"
else
echo " No alert log found" >> "$REPORT_FILE"
fi
echo "📋 Monitoring report generated: $REPORT_FILE"
}
# Main monitoring execution
main() {
load_monitor_config
echo "Starting automation monitoring cycle..."
echo "======================================="
check_automation_jobs
check_system_resources
check_automation_logs
generate_monitoring_report
echo ""
echo "✅ Monitoring cycle completed at $(date)"
echo "📊 Alerts logged to: $ALERT_LOG"
}
# Run monitoring
main "$@"
echo "============================================"
EOF
# Make executable
chmod +x ~/bin/automation_monitor.sh
# Test monitoring system
~/bin/automation_monitor.sh
echo "✅ Automation monitoring system ready!"
What this does: Provides comprehensive monitoring and alerting for automation systems! 📚
🚨 Fix Common Problems
Problem 1: Cron jobs not running ❌
What happened: Scheduled tasks are not executing. How to fix it: Debug cron configuration and permissions!
# Check cron service status
rc-service dcron status
# Restart cron service
rc-service dcron restart
# Check cron logs
tail -f /var/log/cron.log
# Verify crontab syntax
crontab -l | crontab -
# Test script permissions
ls -la ~/automation-scripts/*.sh
chmod +x ~/automation-scripts/*.sh
Problem 2: Scripts failing silently ❌
What happened: Automation scripts run but don’t work properly. How to fix it: Add proper logging and error handling!
# Add logging to scripts
echo "$(date): Script started" >> /var/log/script.log
# Add error handling
set -e # Exit on any error
set -u # Exit on undefined variables
# Test scripts manually
bash -x ~/automation-scripts/script.sh
# Check script dependencies
which apk curl wget rsync
Problem 3: Resource conflicts ❌
What happened: Multiple automation tasks interfere with each other. How to fix it: Implement proper scheduling and locking!
# Add lock files to scripts
LOCKFILE="/tmp/script.lock"
if [ -f "$LOCKFILE" ]; then
echo "Script already running"
exit 1
fi
echo $$ > "$LOCKFILE"
# Trap to remove lock on exit
trap "rm -f $LOCKFILE" EXIT
# Stagger cron job times
# Instead of: 0 2 * * * (all at 2:00)
# Use: 0 2 * * *, 5 2 * * *, 10 2 * * * (spread out)
Don’t worry! Automation troubleshooting takes practice. You’re doing great! 💪
💡 Simple Tips
- Start small 📅 - Begin with simple automation tasks
- Test thoroughly 🌱 - Always test scripts before scheduling
- Monitor closely 🤝 - Watch automation logs regularly
- Document everything 💪 - Keep notes on what each automation does
✅ Check Everything Works
Let’s make sure your automation setup is working:
# Check cron service
rc-service dcron status
# List active cron jobs
crontab -l
# Test automation scripts
cd ~/automation-scripts
ls -la *.sh
# Run automation dashboard
~/bin/automation_dashboard.sh
# Check automation monitoring
~/bin/automation_monitor.sh
# View recent logs
tail -5 /var/log/system_cleanup.log
tail -5 /var/log/smart_automation.log
echo "System automation fully operational! ✅"
Good output:
✅ dcron * service started
✅ 6 active cron jobs configured
✅ All automation scripts executable
📊 System Automation Dashboard loaded
✅ Monitoring system active
✅ Recent automation logs available
System automation fully operational! ✅
🏆 What You Learned
Great job! Now you can:
- ✅ Install and configure cron daemon for task scheduling
- ✅ Create system maintenance automation scripts
- ✅ Set up backup automation and retention policies
- ✅ Implement security monitoring automation
- ✅ Build smart automation that adapts to system conditions
- ✅ Create automation dashboards and monitoring systems
- ✅ Develop custom automation workflows for specific needs
- ✅ Fix common automation issues and conflicts
🎯 What’s Next?
Now you can try:
- 📚 Building distributed automation across multiple Alpine Linux systems
- 🛠️ Implementing automation with configuration management tools like Ansible
- 🤝 Creating automation that integrates with external services and APIs
- 🌟 Building self-healing automation systems that fix problems automatically
Remember: Every expert was once a beginner. You’re doing amazing! 🎉
Keep practicing and you’ll become a system automation expert too! 💫