Schedule tasks with cron jobs and systemd timers
Run scripts on a schedule: the cron syntax explained clearly, typical examples, logging output and errors and when a systemd timer is the better choice.
Backups every night, cleanup every week, a script every five minutes: Linux has two tools for recurring tasks. Cron is simple and known everywhere; systemd timers take a little more effort but catch up on missed runs and log everything.
Cron: edit the crontab
crontab -e # your own tasks
sudo crontab -e # root's tasks
crontab -l # list
The cron syntax
Every line has five time fields followed by the command:
┌ minute (0-59)
│ ┌ hour (0-23)
│ │ ┌ day of month (1-31)
│ │ │ ┌ month (1-12)
│ │ │ │ ┌ weekday (0-7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * * command
| Line | Meaning |
|---|---|
*/5 * * * * |
every 5 minutes |
0 * * * * |
every full hour |
30 3 * * * |
every day at 3:30 |
0 4 * * 1 |
every Monday at 4:00 |
0 2 1 * * |
on the first of every month at 2:00 |
@reboot |
once after every reboot |
The time follows the server's time zone. timedatectl shows it; sudo timedatectl set-timezone Europe/Berlin changes it.
Example: nightly backup
30 3 * * * /usr/local/bin/restic-backup.sh >> /var/log/restic-backup.log 2>&1
>> … 2>&1 writes normal output and errors to a file. Without it they vanish and you never notice that something goes wrong. The script in the example comes from Backups with restic.
Typical pitfalls
- Cron does not know your environment.
PATHis very short. Write programs with their full path (/usr/bin/phpinstead ofphp) or setPATH=/usr/local/bin:/usr/bin:/binat the top of the crontab. %has a special meaning in the crontab. Write\%, for exampledate +\%F.- The script works by hand but not via cron: Usually paths or permissions. Test it once the way cron calls it:
env -i /bin/sh -c '/usr/local/bin/myscript.sh'.
systemd timers: the more robust choice
A timer consists of two files: a service that does the work and a timer that starts it.
sudo nano /etc/systemd/system/restic-backup.service
[Unit]
Description=Nightly restic backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/restic-backup.sh
sudo nano /etc/systemd/system/restic-backup.timer
[Unit]
Description=restic backup every night
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
RandomizedDelaySec=15m
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
systemctl list-timers
The advantages over cron:
| Cron | systemd timer | |
|---|---|---|
| Server was off at the scheduled time | run is skipped | catches up (Persistent=true) |
| Output and errors | only with redirection | automatically in the journal |
| Is the script still running? | not visible | systemctl status restic-backup |
| Start by hand | type the command | sudo systemctl start restic-backup |
| Runs twice at the same time | possible | never |
journalctl -u restic-backup -n 50 shows the log of the last run.
OnCalendar examples
| Value | Meaning |
|---|---|
hourly |
every full hour |
daily |
every day at midnight |
Mon *-*-* 04:00:00 |
Mondays at 4:00 |
*:0/15 |
every 15 minutes |
*-*-01 02:00:00 |
on the first of the month at 2:00 |
Check whether an expression is right with systemd-analyze calendar "Mon *-*-* 04:00:00".
Get notified on failure
So that a failed backup does not stay hidden for weeks, let the script ping a heartbeat monitor at the end, for example with the Statusbot. If the ping does not arrive, you get a message.
For the community
Know your stuff? Write a guide.
Share what you know: we review every submitted guide, publish it under your name and say thank you with credit for your services.