PassioniInformaticaHomelab

Guida: Automazione con cron e systemd timer

04/03/2026

Schedulare task automatici su Linux

Cron e i timer systemd permettono di eseguire script automaticamente a intervalli regolari. Essenziali per backup, monitoraggio e manutenzione.

1. Crontab — sintassi

# Minuto Ora Giorno Mese GiornoSettimana Comando
# *      *   *      *    *
0 3 * * 0 /usr/local/bin/backup.sh          # Domenica alle 3:00
0 */6 * * * /var/www/html/cron/status.php    # Ogni 6 ore
*/15 * * * * /var/www/html/cron/import.php   # Ogni 15 minuti

2. Gestire crontab

crontab -e    # editare
crontab -l    # elencare
crontab -r    # rimuovere tutto

3. File in /etc/cron.d/

# /etc/cron.d/backup
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
0 3 * * 0 root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

4. Systemd timer (alternativa moderna)

# /etc/systemd/system/backup.service
[Unit]
Description=Backup settimanale
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

# /etc/systemd/system/backup.timer
[Unit]
Description=Timer backup settimanale
[Timer]
OnCalendar=Sun *-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
systemctl enable --now backup.timer
systemctl list-timers

5. Logging

# Cron log
grep CRON /var/log/syslog
# Systemd timer log
journalctl -u backup.service

Nell'infrastruttura, cron gestisce: auto-push git (ogni 15 min), import commit (ogni 15 min), status update (ogni 6 ore), backup settimanale.

← Guida: Gitea — self-hosted Git server Guida: Self-hosting email con Postfix e Dovecot →
← Torna all'elenco