Skip to main content

Lab 12.3 - Complete Automation

Objectives​

  • Create a complete automation system
  • Integrate cron with scripts
  • Manage errors
  • Notifications

Exercise 1: Automation Framework​

# Structure
mkdir -p ~/automation/{scripts,logs}

# Main script
cat > ~/automation/automation.sh << 'EOF'
#!/bin/bash
set -euo pipefail
LOG_DIR="$HOME/automation/logs"
LOG="$LOG_DIR/automation-$(date +%Y%m%d).log"

log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG"
}

log "Start automation"
# Tasks...
log "End automation"
EOF

chmod +x ~/automation/automation.sh

Exercise 2: Crontab​

# Add to crontab
crontab -e
# */5 * * * * $HOME/automation/automation.sh

Lab created on: December 2024 - Tested on Ubuntu 24.04