Lab 11.4 - Monitoring Crons and Alerts
Objectives
- Monitor cron execution
- Detect failures
- Configure alerts
- Create reports
Exercise 1: View Cron Logs
# Cron logs
sudo journalctl -u cron
# Filter by script
sudo journalctl -u cron | grep my-script.sh
Exercise 2: Monitoring Script
# Check execution
cat > check-cron.sh << 'EOF'
#!/bin/bash
SCRIPT="my-script.sh"
TODAY=$(date +%b\ %d)
EXECUTED=$(sudo journalctl -u cron | grep "$SCRIPT" | grep "$TODAY" | wc -l)
if [ "$EXECUTED" -eq 0 ]; then
echo "ALERT: $SCRIPT has not been executed today"
fi
EOF
chmod +x check-cron.sh
Exercise 3: Email Alerts
# Configure email in crontab
crontab -e
# Add: MAILTO=your-email@example.com
# Script with notification
cat > script-with-alert.sh << 'EOF'
#!/bin/bash
if ! my-script.sh; then
echo "Failure" | mail -s "Cron Alert" admin@example.com
fi
EOF
Lab created on: December 2024 - Tested on Ubuntu 24.04