Every server ends up with scheduled jobs: backups, certificate checks, log cleanup, reports. On Ubuntu 24.04 and Debian 12 you have two built-in schedulers, cron and systemd timers. Both work. They fail in different ways, and knowing those ways saves you from the classic disaster: a job that silently stopped running months ago.
The short answer
| cron | systemd timer | |
|---|---|---|
| Setup | one line | two small files |
| Logs | wherever you redirect them | journald, automatically |
| Missed runs (server was off) | skipped | can catch up (Persistent=true) |
| Overlap protection | add flock yourself |
built in: one instance at a time |
| Failure hooks |
MAILTO (needs a mail setup) |
OnFailure= any unit |
| Resource limits, sandboxing | no | yes (MemoryMax=, ProtectSystem=...) |
| Random delay | no | RandomizedDelaySec= |
Use cron for quick, simple jobs and for user crontabs. Use systemd timers for anything that matters: backups, jobs that must not overlap, jobs that must run even if the server was down at the scheduled time, and jobs whose failure you want to be told about.
Cron: syntax and the traps
Five time fields, then the command:
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day of month (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ day of week (0-7, 0 and 7 = Sunday)
# │ │ │ │ │
15 3 * * * /usr/local/sbin/backup.sh
*/10 * * * * /usr/local/bin/check-disk.sh
0 9 * * 1-5 /usr/local/bin/weekday-report.sh
Edit your own crontab with crontab -e, list it with crontab -l. Cron is installed on Ubuntu Server and on standard Debian installs; on a minimal image, sudo apt install cron.
Trap 1: the environment is almost empty
Cron runs jobs with a minimal environment. PATH is typically just /usr/bin:/bin, there is no .bashrc, and the shell is /bin/sh, not bash. A script that works in your terminal fails in cron because node, docker or something in /usr/local/bin is not found. Fix it at the top of the crontab, or use absolute paths everywhere:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Trap 2: % is special
In a crontab line, % means "newline". This breaks silently:
0 2 * * * tar czf /var/backups/www-$(date +%F).tar.gz /var/www # broken
0 2 * * * tar czf /var/backups/www-$(date +\%F).tar.gz /var/www # works
Better: put the logic in a script and call the script.
Trap 3: /etc/cron.d has its own rules
System-wide files in /etc/cron.d/ have an extra field, the user, between the schedule and the command:
# /etc/cron.d/backup
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
15 3 * * * root /usr/local/sbin/backup.sh >> /var/log/backup.log 2>&1
Two more rules that cause "my job never runs" tickets: the file name must not contain dots (on Debian and Ubuntu, backup.cron is ignored, backup works), and the file must end with a newline. Files must be owned by root and not writable by group or others.
Trap 4: output goes nowhere
Without redirection, cron tries to mail the output to the user. On most servers no mail system is configured, so the output, including your error messages, is lost. MAILTO=you@example.com only helps if the server can actually send mail. The reliable option is to redirect to a log file, as above, or to the journal:
15 3 * * * root /usr/local/sbin/backup.sh 2>&1 | systemd-cat -t backup
Then journalctl -t backup shows every run.
Trap 5: overlapping runs
If a job sometimes takes longer than its interval, cron starts a second copy anyway. Two backups writing to the same repository at once is a bad day. Wrap it in flock:
*/10 * * * * root flock -n /run/sync.lock /usr/local/bin/sync.sh
-n means "if the lock is held, skip this run" instead of queueing.
systemd timers: the same job, done properly
A timer is two units: a .service that says what to run and a .timer that says when.
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
OnFailure=notify-failure@%n.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/backup.sh
Nice=10
IOSchedulingClass=idle
# /etc/systemd/system/backup.timer
[Unit]
Description=Run the nightly backup
[Timer]
OnCalendar=*-*-* 03:15:00
Persistent=true
RandomizedDelaySec=15m
[Install]
WantedBy=timers.target
Enable the timer, not the service:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers
What you get for free:
-
Persistent=true: if the server was off at 03:15, the job runs soon after the next boot instead of being skipped. Cron simply misses it. -
RandomizedDelaySec=15m: spreads the start over 15 minutes. Useful when many servers would otherwise hit the same backup target at the same second. - No overlap: a service that is still running is not started again.
-
Logs: everything the script prints lands in the journal:
journalctl -u backup.service, orjournalctl -u backup.service --since today. -
Exit status:
systemctl status backup.serviceshows whether the last run succeeded.
Run it by hand anytime, the exact way the timer would: sudo systemctl start backup.service.
OnCalendar syntax
The format is DayOfWeek Year-Month-Day Hour:Minute:Second, with * as a wildcard. Do not guess; test your expression:
systemd-analyze calendar "Mon..Fri 09:00"
systemd-analyze calendar "*-*-* 03:15:00" --iterations=3
systemd-analyze calendar "*:0/10" # every 10 minutes
The output shows the normalized form and the next elapse times. Shortcuts like hourly, daily, weekly and monthly also work. For "every N minutes after the last run" instead of wall-clock times, use OnUnitActiveSec=10min together with OnBootSec=2min.
Getting told when it fails
OnFailure= starts another unit when the service fails. A small template that sends a push notification:
# /etc/systemd/system/notify-failure@.service
[Unit]
Description=Failure notice for %i
[Service]
Type=oneshot
ExecStart=/usr/bin/curl -fsS -m 10 -d "Job failed: %i on %H" https://ntfy.example.com/my-alerts
%i is the name of the failed unit and %H the host name. Swap the curl line for whatever you use: a webhook, a chat bot or mail.
Hardening comes cheap
Because the job is a systemd service, you can restrict it in a few lines:
[Service]
User=backup
ProtectSystem=strict
ReadWritePaths=/var/backups
PrivateTmp=true
NoNewPrivileges=true
MemoryMax=1G
A buggy or compromised job now cannot write outside /var/backups or eat all the RAM.
Migrating a cron job
- Move the command into a script with
set -euo pipefailat the top, so failures produce a non-zero exit. - Create the
.service(Type=oneshot) and.timerpair. - Check the schedule with
systemd-analyze calendar. -
systemctl startthe service once by hand and readjournalctl -u. - Enable the timer, then delete the cron line, so the job does not run twice.
Watching the scheduler itself
Both systems share one blind spot: if the job never starts, nothing complains. Add a dead man's switch. Have the job call a heartbeat URL at the end (a "push" monitor in Uptime Kuma or a similar tool) and get alerted when the heartbeat stops arriving.
Further reading
I work with PhantomVPS; its blog has a shorter, beginner-level guide to cron jobs on a VPS. For the full reference, see man 5 crontab, man systemd.timer and man systemd.time on your server.
Top comments (0)