Sub topic 5
Choosing between cron and systemd timers
This chapter assumes you can write a basic crontab entry and read a systemd unit file, as covered in the two preceding chapters. It compares the two schedulers and gives you criteria for choosing one. It also covers the errors people commonly make when they switch from one to the other.
Two schedulers, two models
Cron is a daemon that wakes up every minute, reads its crontab files, and runs any command whose five time fields match the current minute. It has no concept of a job's state. It starts the command, and what happens afterwards is up to you.
A systemd timer is a unit that activates another unit, normally a service of the same name. The timer only decides when. The service defines how the job runs: user, environment, limits, timeouts, and dependencies. Because the job is a real unit, systemd tracks its state, exit status, and logs.
This difference explains most of the trade-offs below.
Comparison
| Concern | cron | systemd timer |
|---|---|---|
| Setup effort | One line in a crontab | Two unit files, then `daemon-reload` and `enable` |
| Environment | Minimal, set by hand | Clean and defined by the unit; no login shell |
| Logs | Mail or redirected files | Journal, queried with `journalctl -u` |
| Job status | None kept | `systemctl status`, last result, next run |
| Overlap | Possible; needs `flock` | A service unit is not started again while it is still running |
| Missed runs | Skipped (anacron is a separate tool) | `Persistent=true` catches up |
| Timeouts | Wrap with `timeout` | `TimeoutStartSec=` |
| Sandboxing | None built in | Directives such as `User=`, `ProtectSystem=`, `PrivateTmp=` |
| Randomized start | Manual `sleep` | `RandomizedDelaySec=` |
| Portability | Almost every Unix | Linux distributions with systemd |
Where cron is the better choice
- Quick, simple jobs. A one-line entry for a small task is hard to beat. Creating two unit files for a job that rotates a scratch directory is more ceremony than the job deserves.
- Portability. If the same scripts must run on Alpine containers, BSD hosts, or older systems without systemd, cron is the common denominator.
- Per-user scheduling without root. Any user can run
crontab -e. User timers exist (systemctl --user), but they need a user manager that is running, and that depends on a login session or onloginctl enable-linger. - Existing estate. If a team already has monitoring, reviews, and habits built around crontabs, converting for its own sake adds risk and little benefit.
Where systemd timers are the better choice
- Visibility.
systemctl list-timersshows the last and next run of every timer.systemctl status backup.serviceshows the last exit code. With cron you must build this yourself. - Logging. Output goes to the journal automatically, tagged by unit, with no redirect syntax to get wrong.
- Resource and security controls. Because the job is a service, you can set
TimeoutStartSec,MemoryMax,Nice, and hardening options without wrapper scripts. - Catch-up after downtime.
Persistent=truerecords the last activation on disk and runs a missed activation at boot. Cron simply skips runs that fell while the machine was off. - Dependencies. A job can declare
After=network-online.targetorRequires=a mount, and systemd orders the start accordingly. - Spreading load.
RandomizedDelaySec=stops a fleet of servers from hitting a shared backup target at exactly 02:30.
Gotchas when moving between them
Translating the schedule
Cron's */15 * * * * becomes OnCalendar=*:0/15. The two syntaxes are not identical, so do not translate by eye. Check with systemd-analyze calendar:
systemd-analyze calendar "*:0/15"
systemd-analyze calendar --iterations=5 "Mon..Fri *-*-* 09:00:00"The output prints the normalized form and the next elapsed time. This is the quickest way to catch a schedule that does not mean what you thought.
Day-of-month and day-of-week
In cron, if both the day-of-month and day-of-week fields are restricted, the job runs when either matches. 0 3 1 * 1 runs on the first of the month and every Monday. In OnCalendar, the weekday and the date are combined with AND. Mon *-*-01 03:00 runs only on a Monday that is also the first. If you port a crontab with both fields set, the schedule changes meaning.
Overlap is handled differently
Cron starts a new process every time, even when the previous one is alive. The flock -n wrapper from the earlier chapter is the fix.
A timer activates its service. If a oneshot service is still running when the timer fires again, the start request does not launch a second copy, because the unit is already in the activating state. The later activation is not queued. You still need to think about the result: a job that takes longer than its interval silently skips runs. Check that the run time fits the interval, and alert on it.
Time-based triggers are not all calendar-based
systemd also has monotonic timers: OnBootSec= and OnUnitActiveSec=. They measure from an event, not from the clock. A timer with OnUnitActiveSec=15min waits 15 minutes after the last activation, so it never drifts into overlap the way a fixed-clock schedule can. Cron has only wall-clock schedules, plus the non-standard @reboot.
The environment differs in both directions
Cron gives a minimal PATH and a /bin/sh shell. A systemd service starts with a clean environment as well, and it does not read ~/.bashrc or /etc/profile. A script that worked from your shell can fail in either scheduler. Set Environment= or EnvironmentFile= in the unit, and call binaries by absolute path.
Forgotten steps
After you create or edit a unit file, run systemctl daemon-reload. After you create a timer, enable and start the timer, not the service:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timerEnabling backup.service instead does nothing useful for a scheduled job. A common mistake is to enable the timer and never confirm that list-timers shows a NEXT value.
Decision criteria
Ask these questions in order:
- Must it run on systems without systemd? If yes, use cron.
- Does it need catch-up after downtime, a timeout, resource limits, sandboxing, or ordering against other units? If yes, use a systemd timer.
- Do you need to see last result and next run without building tooling? If yes, use a systemd timer.
- Is it a trivial, low-stakes, single-line task on a machine already managed with cron? Use cron.
- Otherwise, default to a systemd timer on a modern Linux server, and to cron on mixed estates where consistency matters more than features.
You can also mix them. Many servers run distribution-supplied cron jobs and systemd timers side by side. The risk is not the mix. It is that nobody knows where a job lives. Keep a list of where each job is defined, and check both places during an audit:
systemctl list-timers --all
sudo crontab -l
sudo ls /etc/cron.d /etc/cron.daily /var/spool/cron 2>/dev/nullExample: the same job both ways
The job is a sync script that must run every 15 minutes without overlap.
With cron:
*/15 * * * * /usr/bin/flock -n /run/lock/sync.lock /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1With systemd:
# /etc/systemd/system/sync.service
[Unit]
Description=Sync job
[Service]
Type=oneshot
User=sync
ExecStart=/usr/local/bin/sync.sh
TimeoutStartSec=10min
# /etc/systemd/system/sync.timer
[Unit]
Description=Run sync every 15 minutes
[Timer]
OnCalendar=*:0/15
Persistent=true
[Install]
WantedBy=timers.targetThe systemd version is longer, but it adds a timeout, a dedicated user, journal logging, and catch-up after downtime. The overlap protection comes from the unit itself, so no flock is needed there. The cron version is one line, and you build every extra feature yourself.
Exercise
A crontab entry reads 0 4 15 * 5 /usr/local/bin/report.sh. You convert it to a timer with OnCalendar=Fri *-*-15 04:00:00 and find that the report runs far less often than before. Why, and how would you reproduce the old behaviour with systemd?
Show answer
In cron, when both day-of-month and day-of-week are restricted, the job runs when either matches: on the 15th of every month and on every Friday. In OnCalendar the two conditions are combined, so the timer fires only on a Friday that is also the 15th. To reproduce the old behaviour, use two OnCalendar= lines in the same [Timer] section, for example OnCalendar=Fri *-*-* 04:00:00 and OnCalendar=*-*-15 04:00:00. Confirm both with systemd-analyze calendar. If a day matches both, the service is activated once for that moment.
Exercise
A server is powered off every night from 01:00 to 06:00. A job scheduled for 02:30 must still run once each day. Which scheduler features make that work, and what would you add to cron to get similar behaviour?
Show answer
A systemd timer with OnCalendar=*-*-* 02:30:00 and Persistent=true records the last activation on disk and runs the missed activation when the machine boots. Plain cron skips a run that fell while the machine was off. You would need anacron, which runs daily, weekly, or monthly jobs at the next opportunity, or an @reboot entry combined with a check that the job has not already run today.