# crontab — Befehle zeitgesteuert per cron-Daemon ausführen

> Praxis-Guide zu crontab: Jobs zeitgesteuert per cron planen, Cron-Syntax, Logging und Umgebung verstehen – und die Falle crontab -r vermeiden.

Source: https://www.jpkc.com/db/cheatsheets/shell-system/crontab/

<!-- PROSE:intro -->
crontab ist der klassische Job-Scheduler unter Unix und Linux: Du hinterlegst Befehle mit fünf Zeitfeldern, und der cron-Daemon führt sie zuverlässig im Hintergrund aus – von der nächtlichen Sicherung bis zum Health-Check alle fünf Minuten. Tückisch ist vor allem die Umgebung: Cron startet Jobs mit minimalem `PATH` und ohne dein Login-Profil, ein literales `%` im Befehl muss escaped werden, und `crontab -r` löscht ohne Rückfrage deine komplette Crontab – gefährlich nah an `crontab -e`. Dieser Guide zeigt dir die Befehle, die Syntax und die Stolperfallen.
<!-- PROSE:intro:end -->

## Crontabs verwalten

`crontab -e` — Bearbeitet die Crontab des aktuellen Nutzers im Standard-Editor.

```bash
crontab -e
```

`crontab -l` — Listet die Crontab-Einträge des aktuellen Nutzers auf (zeigt sie an).

```bash
crontab -l
```

`crontab -r` — Entfernt (löscht) die gesamte Crontab des aktuellen Nutzers. Achtung: keine Rückfrage und leicht mit `crontab -e` zu verwechseln – sichere sie vorher mit `crontab -l > backup.txt`.

```bash
crontab -r
```

`crontab -u <user> -e` — Bearbeitet die Crontab eines anderen Nutzers (erfordert root).

```bash
sudo crontab -u www-data -e
```

`crontab -u <user> -l` — Listet die Crontab-Einträge eines anderen Nutzers auf.

```bash
sudo crontab -u deploy -l
```

`crontab <file>` — Installiert eine Crontab aus einer Datei (ersetzt die bestehende Crontab).

```bash
crontab mycron.txt
```

`crontab -l > <file>` — Sichert die aktuelle Crontab in eine Datei.

```bash
crontab -l > crontab-backup.txt
```

## Cron-Syntax

`* * * * * <command>` — Format: Minute (0-59) Stunde (0-23) Tag des Monats (1-31) Monat (1-12) Wochentag (0-7, 0 und 7 = Sonntag).

```bash
30 2 * * * /usr/local/bin/backup.sh
```

`*/n` — Schrittwert: läuft alle n Intervalle.

```bash
*/5 * * * * (alle 5 Minuten)
```

`n,n,n` — Liste: läuft zu bestimmten Werten.

```bash
0 8,12,18 * * * (um 8:00, 12:00, 18:00)
```

`n-n` — Bereich: läuft für alle Werte im Bereich.

```bash
0 9-17 * * 1-5 (jede Stunde 9-17 Uhr, Mo-Fr)
```

`n-n/s` — Bereich mit Schritt: läuft jeden s-ten Wert innerhalb des Bereichs.

```bash
0-30/10 * * * * (zu Minute 0, 10, 20, 30)
```

## Spezielle Schlüsselwörter

`@reboot <command>` — Läuft einmal beim Systemstart.

```bash
@reboot /home/user/start-service.sh
```

`@yearly <command>` — Läuft einmal im Jahr (entspricht 0 0 1 1 *).

```bash
@yearly /usr/local/bin/annual-report.sh
```

`@monthly <command>` — Läuft einmal im Monat (entspricht 0 0 1 * *).

```bash
@monthly /usr/local/bin/monthly-cleanup.sh
```

`@weekly <command>` — Läuft einmal pro Woche (entspricht 0 0 * * 0).

```bash
@weekly /usr/local/bin/weekly-backup.sh
```

`@daily <command>` — Läuft einmal am Tag (entspricht 0 0 * * *).

```bash
@daily /usr/local/bin/logrotate.sh
```

`@hourly <command>` — Läuft einmal pro Stunde (entspricht 0 * * * *).

```bash
@hourly /usr/local/bin/check-health.sh
```

## Häufige Zeitpläne

`* * * * *` — Jede Minute.

```bash
* * * * * /usr/local/bin/monitor.sh
```

`*/5 * * * *` — Alle 5 Minuten.

```bash
*/5 * * * * /usr/local/bin/check-queue.sh
```

`*/15 * * * *` — Alle 15 Minuten.

```bash
*/15 * * * * /usr/local/bin/sync-data.sh
```

`0 * * * *` — Jede Stunde (zur Minute 0).

```bash
0 * * * * /usr/local/bin/hourly-report.sh
```

`0 0 * * *` — Jeden Tag um Mitternacht.

```bash
0 0 * * * /usr/local/bin/daily-backup.sh
```

`0 2 * * *` — Jeden Tag um 2:00 Uhr.

```bash
0 2 * * * /usr/local/bin/maintenance.sh
```

`0 9-17 * * 1-5` — Jede Stunde während der Geschäftszeiten (Mo-Fr, 9-17 Uhr).

```bash
0 9-17 * * 1-5 /usr/local/bin/business-check.sh
```

`0 0 * * 0` — Jeden Sonntag um Mitternacht.

```bash
0 0 * * 0 /usr/local/bin/weekly-cleanup.sh
```

`0 0 1 * *` — Am ersten Tag jedes Monats um Mitternacht.

```bash
0 0 1 * * /usr/local/bin/monthly-report.sh
```

`30 4 1,15 * *` — Um 4:30 Uhr am 1. und 15. jedes Monats.

```bash
30 4 1,15 * * /usr/local/bin/biweekly-task.sh
```

## Ausgabe & Logging

`* * * * * <command> >> /var/log/cron.log 2>&1` — Leitet stdout und stderr in eine Log-Datei um (anhängen).

```bash
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
```

`* * * * * <command> > /dev/null 2>&1` — Unterdrückt jegliche Ausgabe (keine E-Mail, kein Log).

```bash
*/5 * * * * /usr/local/bin/check.sh > /dev/null 2>&1
```

`MAILTO="user@example.com"` — Setzt den E-Mail-Empfänger für die Cron-Ausgabe (an den Anfang der Crontab stellen).

```bash
MAILTO="admin@example.com"
```

`MAILTO=""` — Deaktiviert E-Mail-Benachrichtigungen für alle Cron-Jobs.

```bash
MAILTO=""
```

`* * * * * <command> 2>&1 | logger -t mycron` — Schickt die Ausgabe mit eigenem Tag ans Syslog.

```bash
0 * * * * /usr/local/bin/task.sh 2>&1 | logger -t hourly-task
```

## Umgebungsvariablen

`PATH=/usr/local/bin:/usr/bin:/bin` — Setzt den PATH für Cron-Jobs (Cron nutzt standardmäßig einen minimalen PATH).

```bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
```

`SHELL=/bin/bash` — Setzt die Shell, mit der Cron-Befehle ausgeführt werden (Standard ist /bin/sh).

```bash
SHELL=/bin/bash
```

`HOME=/home/user` — Setzt das Home-Verzeichnis für Cron-Jobs.

```bash
HOME=/home/deploy
```

`* * * * * . /home/user/.profile && <command>` — Lädt das Profil des Nutzers, um vor dem Ausführen die volle Umgebung zu bekommen.

```bash
0 * * * * . /home/deploy/.profile && /usr/local/bin/task.sh
```

`* * * * * env VAR=value <command>` — Setzt eine Variable inline für einen einzelnen Cron-Job.

```bash
0 2 * * * env NODE_ENV=production /usr/local/bin/node /app/worker.js
```

## System-Cron-Verzeichnisse

`/etc/crontab` — Systemweite Crontab-Datei. Hat nach den Zeitfeldern ein zusätzliches Nutzer-Feld.

```bash
0 * * * * root /usr/local/bin/system-check.sh
```

`/etc/cron.d/` — Verzeichnis für weitere systemweite Crontab-Dateien (gleiches Format wie /etc/crontab).

```bash
ls /etc/cron.d/
```

`/etc/cron.hourly/` — Skripte in diesem Verzeichnis werden stündlich von anacron/run-parts ausgeführt.

```bash
cp myscript.sh /etc/cron.hourly/
```

`/etc/cron.daily/` — Skripte laufen einmal am Tag.

```bash
ls /etc/cron.daily/
```

`/etc/cron.weekly/` — Skripte laufen einmal pro Woche.

```bash
ls /etc/cron.weekly/
```

`/etc/cron.monthly/` — Skripte laufen einmal im Monat.

```bash
ls /etc/cron.monthly/
```

## Fehlersuche

`grep CRON /var/log/syslog` — Durchsucht das Syslog nach Cron-Ausführungsprotokollen.

```bash
grep CRON /var/log/syslog | tail -20
```

`journalctl -u cron` — Zeigt Cron-Logs über das systemd-Journal an.

```bash
journalctl -u cron --since today
```

`systemctl status cron` — Prüft, ob der cron-Daemon läuft.

```bash
systemctl status cron
```

`run-parts --test /etc/cron.daily` — Testet, welche Skripte in einem Cron-Verzeichnis ausgeführt würden.

```bash
run-parts --test /etc/cron.daily
```

`* * * * * /usr/bin/env > /tmp/cron-env.txt` — Debuggt die Cron-Umgebung, indem alle Umgebungsvariablen ausgegeben werden.

```bash
* * * * * /usr/bin/env > /tmp/cron-env.txt 2>&1
```

`cat /etc/cron.allow` — Zeigt, welche Nutzer crontab verwenden dürfen (falls die Datei existiert).

```bash
cat /etc/cron.allow
```

`cat /etc/cron.deny` — Zeigt, welchen Nutzern der crontab-Zugriff verwehrt ist (falls die Datei existiert).

```bash
cat /etc/cron.deny
```

<!-- PROSE:outro -->
## Fazit

crontab erledigt wiederkehrende Aufgaben zuverlässig im Hintergrund – im Alltag kommst du mit `crontab -e` zum Bearbeiten, `crontab -l` zum Anzeigen und der vertrauten Syntax aus fünf Zeitfeldern weit. Die häufigsten Probleme liegen nicht in der Syntax, sondern in der Umgebung: Cron startet Jobs mit einem minimalen `PATH` und ohne dein Login-Profil, deshalb solltest du absolute Pfade verwenden oder `PATH` explizit setzen. Ein literales `%` im Befehl wird von cron als Zeilenumbruch interpretiert und muss als `\%` escaped werden. Logge die Ausgabe (`>> logdatei 2>&1`) oder setze `MAILTO`, sonst gehen Fehler unbemerkt verloren. Und Vorsicht bei `crontab -r`: Es löscht ohne Rückfrage die komplette Crontab und sitzt auf der Tastatur direkt neben `crontab -e` – sichere vorher mit `crontab -l > backup.txt`.

## Weiterführende Links

- [ubuntuusers-Wiki: Cron](https://wiki.ubuntuusers.de/Cron/) – ausführliche deutschsprachige Anleitung zu crontab und den System-Cron-Verzeichnissen
- [Wikipedia: cron](https://de.wikipedia.org/wiki/Cron) – Hintergrund zu cron, der Syntax und den Implementierungen
<!-- PROSE:outro:end -->

## Verwandte Kommandos

- [at](https://www.jpkc.com/db/cheatsheets/shell-system/at/) – plant Einmal-Jobs zu einem bestimmten Zeitpunkt statt wiederkehrend
- [systemctl](https://www.jpkc.com/db/cheatsheets/shell-system/systemctl/) – steuert systemd-Dienste und -Timer als moderne Cron-Alternative
- [journalctl](https://www.jpkc.com/db/cheatsheets/shell-system/journalctl/) – durchsucht das systemd-Journal, etwa nach Cron-Ausführungen

