← Zurück

Cron Jobs

Cron Jobs – Cheatsheet

Schnellreferenz für zeitgesteuerte Aufgaben unter Linux.


Crontab-Syntax

┌─────── Minute (0-59)
│ ┌───── Stunde (0-23)
│ │ ┌─── Tag (1-31)
│ │ │ ┌─ Monat (1-12)
│ │ │ │ ┌ Wochentag (0-7, So=0 oder 7)
│ │ │ │ │
* * * * * Befehl

Sonderzeichen

Zeichen Bedeutung Beispiel
* Jeder Wert * * * * *
, Liste 1,15,30
- Bereich 1-5 (Mo-Fr)
/ Intervall */15 (alle 15)

Häufige Beispiele

# Jede Minute
* * * * * /script.sh

# Alle 5 Minuten
*/5 * * * * /script.sh

# Alle 15 Minuten
*/15 * * * * /script.sh

# Jede Stunde
0 * * * * /script.sh

# Alle 2 Stunden
0 */2 * * * /script.sh

# Täglich um 3:00
0 3 * * * /script.sh

# Täglich um 3:30
30 3 * * * /script.sh

# Wochentags 9:00
0 9 * * 1-5 /script.sh

# Jeden Montag 8:00
0 8 * * 1 /script.sh

# Jeden Sonntag 2:00
0 2 * * 0 /script.sh

# 1. des Monats
0 0 1 * * /script.sh

# Zweimal täglich
0 8,20 * * * /script.sh

Spezielle Strings

@reboot     # Beim Systemstart
@hourly     # Stündlich (0 * * * *)
@daily      # Täglich (0 0 * * *)
@weekly     # Wöchentlich (0 0 * * 0)
@monthly    # Monatlich (0 0 1 * *)
@yearly     # Jährlich (0 0 1 1 *)

Crontab-Befehle

# Bearbeiten
crontab -e

# Anzeigen
crontab -l

# Löschen
crontab -r

# Anderer User (als root)
sudo crontab -e -u username
sudo crontab -l -u username

Umgebungsvariablen

# Am Anfang der Crontab
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=admin@example.com

# Keine Emails
MAILTO=""

Output umleiten

# In Logdatei
0 3 * * * /script.sh >> /var/log/cron.log 2>&1

# Nur Fehler loggen
0 3 * * * /script.sh > /dev/null 2>> /var/log/error.log

# Komplett stumm
0 3 * * * /script.sh > /dev/null 2>&1

System-Verzeichnisse

/etc/crontab          # System-Crontab
/etc/cron.d/          # Zusätzliche Crontabs
/etc/cron.hourly/     # Stündliche Scripts
/etc/cron.daily/      # Tägliche Scripts
/etc/cron.weekly/     # Wöchentliche Scripts
/etc/cron.monthly/    # Monatliche Scripts

Troubleshooting

# Cron-Dienst läuft?
systemctl status cron

# Logs prüfen
grep CRON /var/log/syslog
journalctl -u cron

# Script testen
/bin/bash -c "/pfad/script.sh"

# Umgebung im Script prüfen
env > /tmp/cron-env.txt

Häufige Fehler

Problem Lösung
Läuft nicht Absolute Pfade!
Keine Ausgabe >> logfile 2>&1
Permission denied chmod +x script.sh
Falscher Pfad which befehl prüfen

at (Einmalige Ausführung)

# In 5 Minuten
echo "/script.sh" | at now + 5 minutes

# Bestimmte Zeit
at 15:30 <<< "/script.sh"

# Jobs anzeigen
atq

# Job löschen
atrm 5