Practical Automation Tasks
System Management Automation
Disk Space Monitoring Script
#!/bin/bash
## LabEx Disk Space Monitor
THRESHOLD=80
df -h | grep '/$' | awk '{print $5}' | cut -d'%' -f1 | while read usage; do
if [ $usage -ge $THRESHOLD ]; then
echo "Warning: Disk usage is $usage%"
## Send alert or take action
fi
done
Automated Backup Script
#!/bin/bash
## LabEx Backup Automation
BACKUP_DIR="/home/user/backups"
SOURCE_DIR="/home/user/important_data"
DATE=$(date +"%Y%m%d")
mkdir -p $BACKUP_DIR
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" $SOURCE_DIR
Log Management
Log Rotation Script
#!/bin/bash
## LabEx Log Rotation
LOG_DIR="/var/log/labex"
MAX_LOGS=5
cd $LOG_DIR
ls -t *.log | tail -n +$((MAX_LOGS + 1)) | xargs -I {} rm {}
Network Automation
Connectivity Check Script
#!/bin/bash
## LabEx Network Connectivity Monitor
HOSTS=("8.8.8.8" "github.com" "labex.io")
for host in "${HOSTS[@]}"; do
if ping -c 4 $host > /dev/null 2>&1; then
echo "$host is reachable"
else
echo "Warning: $host is not reachable"
fi
done
Automation Task Categories
graph TD
A[Automation Tasks] --> B[System Management]
A --> C[Log Management]
A --> D[Network Monitoring]
A --> E[Security Checks]
A --> F[Backup Processes]
User Management Automation
User Creation Script
#!/bin/bash
## LabEx User Management
create_user() {
username=$1
password=$2
if id "$username" &> /dev/null; then
echo "User $username already exists"
return 1
fi
useradd -m $username
echo "$username:$password" | chpasswd
echo "User $username created successfully"
}
## Example usage
create_user "labex_user" "StrongPassword123"
Automation Task Types
| Category |
Purpose |
Example Tasks |
| System |
Resource Management |
Disk cleanup, process monitoring |
| Network |
Connectivity |
Ping checks, bandwidth monitoring |
| Security |
System Protection |
Log analysis, vulnerability scanning |
| Backup |
Data Preservation |
Automated backups, archiving |
| Maintenance |
System Health |
Update checks, performance logging |
System Resource Script
#!/bin/bash
## LabEx Resource Monitor
log_system_resources() {
echo "--- System Resources $(date) ---"
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4 "%"}'
echo "Memory Usage:"
free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }'
echo "Disk Usage:"
df -h / | awk '/\// {print $5}'
}
## Run and log resources
log_system_resources >> /var/log/labex/resource_monitor.log
Best Practices
- Use error handling
- Implement logging
- Create modular scripts
- Use configuration files
- Test thoroughly
- Secure sensitive information
Advanced Automation Considerations
- Implement error notifications
- Use configuration management
- Create flexible, reusable scripts
- Consider security implications
- Maintain documentation