Automation

Scripting, scheduling, and configuration management.

Topics

Topic Description

Bash Scripting

Best practices, error handling, common patterns

systemd Timers

Scheduled tasks, calendar expressions

Ansible

Playbooks, roles, inventory, idempotency

systemd Timers

systemd timers replace cron with better logging and dependency management.

Example Timer

# /etc/systemd/system/backup.timer
[Unit]
Description=Daily backup timer

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target
# /etc/systemd/system/backup.service
[Unit]
Description=Backup service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

Timer Commands

systemctl enable --now backup.timer
systemctl list-timers
systemctl status backup.timer
journalctl -u backup.service

Bash Best Practices

#!/usr/bin/env bash
set -euo pipefail

# Fail on:
# -e: any command error
# -u: undefined variables
# -o pipefail: pipe failures

readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly LOG_FILE="/var/log/script.log"

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

main() {
    log "Starting..."
    # Your code here
    log "Complete"
}

main "$@"