
Recall first from cron and how to debug it
- cron runs jobs with a minimal environment; use absolute paths or set PATH yourself.
- An unescaped % in a crontab command becomes a newline and feeds the rest to stdin.
- If both day-of-month and day-of-week are restricted, cron runs the job when either matches, not both.
systemd timers: schedules as units
4 min readcron assumes an always-on machine with a correct clock and precise times. This lesson answers what to use for schedules cron cannot cover: systemd timers with catch-up, one-shot at jobs, and anacron's day counting.
On any modern systemd system, timers are the native scheduler. You met units in the boot lesson; a timer is just another unit type. A timer named backup.timer activates backup.service by default, so the pair shares one base name. The service says what runs and as whom; the timer says when.
1Write the service and the timer
Put both files in /etc/systemd/system/. First the service:
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly data backup
[Service]
Type=oneshot
User=learner
ExecStart=/home/learner/bin/backup.sh
Type=oneshot means the service runs a task and finishes, rather than staying up like a server. Now the timer:
# /etc/systemd/system/backup.timer
[Unit]
Description=Run the backup every night
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target
OnCalendar takes a calendar expression. *-*-* 02:30:00 means every day at 02:30. Mon..Fri *-*-* 09:00:00 means weekdays at 09:00. *-*-01 00:00:00 means midnight on the first of each month. *:0/15 means every 15 minutes. Shortcuts like daily, weekly, and hourly also work.
Persistent=true is the headline feature. If the machine was off at 02:30, a plain cron job is simply lost. A persistent timer records when it last fired and runs the job once, soon after the next boot, to catch up. That makes timers ideal for laptops and boards that sleep or lose power.
For intervals measured from events rather than the wall clock, use OnBootSec=10min (after boot) or OnUnitActiveSec=1h (after the service last activated). These monotonic timers do not care what the calendar says, which matters on boards with no reliable clock.
2Enable, list, and inspect
Reload systemd's view of the files, then enable the timer (not the service):
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers --all
Typical output (yours will differ):
NEXT LEFT LAST PASSED UNIT ACTIVATES
Tue 2026-11-17 02:30:00 CET 7h left Mon 2026-11-16 02:30:03 CET 16h ago backup.timer backup.service
NEXT and LEFT show the upcoming run; LAST and PASSED show the previous one. The service's output goes to the journal, so debugging is journalctl -u backup.service, with no mail setup and no redirection. To check a calendar expression before trusting it, ask systemd to parse it:
systemd-analyze calendar 'Mon..Fri *-*-* 09:00:00'
systemd-analyze calendar --iterations=3 '*:0/15'
The first command prints the normalized form and the next time it matches; the second lists the next three matches. If the expression is invalid, it says so instead of silently never firing.
3Tune timing and compare with cron
By default a timer may fire up to one minute late, because systemd coalesces timers to save wake-ups (AccuracySec=1min). RandomizedDelaySec=5min adds a random delay up to five minutes, which spreads load when many machines run the same job. cron has neither feature built in; people fake a random delay by starting the job with a sleep of a random length.
Timers also inherit everything services give you: User=, resource limits through cgroups, After=network-online.target for ordering, and OnFailure= to trigger an alert unit when the job fails. And a timer never starts a second copy of a service that is still running, so overlap is prevented by design. cron will happily start a new copy while the old one still runs.
at and anacron: one-shots and catch-up
3 min read1Queue a one-shot job with at
at submits a single future job to the atd daemon. You give it a time and a command:
at now + 1 hour
at 23:00
at -f /home/learner/bin/upgrade.sh 02:00 tomorrow
The first form reads commands from your terminal until you press Ctrl-D; -f reads them from a file. Times can be creative: now + 5 minutes, teatime (16:00), midnight fri. atq lists queued jobs with their numbers, and atrm 3 removes job 3. at -c 3 prints the job, including the full environment it captured when you submitted it.
That capture is the key difference from cron. An at job inherits your current environment, so your PATH and variables travel with it. A cron job gets the minimal environment. at also mails output by default, and if the machine was off at the appointed time, atd runs the overdue job when it next starts. batch is a variant that waits until the load average falls below a threshold (1.5 by default) instead of running at a fixed time; it suits heavy one-off work on a shared machine.
Use at for "run this upgrade tonight", not for recurring maintenance. Recurring jobs belong in cron or a timer, where the schedule is visible in one file.
2Let anacron handle machines that sleep
cron assumes the machine is on at the scheduled minute. A laptop or a solar-powered board is not. anacron solves this by counting days, not clock times. Its table, /etc/anacrontab, has four fields: period in days, delay in minutes, a job identifier, and the command. A typical Debian or Ubuntu table looks like this:
RANDOM_DELAY=45
START_HOURS_RANGE=3-22
1 5 cron.daily nice run-parts /etc/cron.daily
7 10 cron.weekly nice run-parts /etc/cron.weekly
30 15 cron.monthly nice run-parts /etc/cron.monthly
The first job line says: run the daily scripts every 1 day, waiting 5 minutes after anacron starts. anacron keeps a timestamp file per job under /var/spool/anacron/. At startup it compares each timestamp with today; if the period has elapsed, the job is due. It runs due jobs one after another, never in parallel, and adds a random delay up to RANDOM_DELAY minutes. START_HOURS_RANGE keeps jobs inside reasonable hours. On systemd systems, anacron itself is started by a service and timer, so the chain is: timer starts anacron, anacron runs the daily scripts.
This is why /etc/crontab defers to anacron when it is installed. The division of labour: cron for precise clock times on always-on machines, anacron for daily-or-longer chores on machines that come and go, timers when you want both precision and catch-up in one mechanism.
Many embedded images use BusyBox crond instead of full cron. Start it with crond -c /etc/crontabs -L /var/log/cron.log (or -f to keep it in the foreground under a supervisor). It reads per-user tables and supports the five fields, but it sends no mail, and support for @daily-style strings depends on how BusyBox was built. Capture output yourself. Also, boards without a battery-backed clock boot with the wrong time until NTP fixes it, so wall-clock schedules can misfire; monotonic timers like OnBootSec= are immune, and time-sync.target lets a unit wait for a correct clock.
Key takeaways
- A systemd timer pairs a .timer with a .service; Persistent=true catches up missed runs.
- anacron counts days, not clock times, so it suits machines that are not always on.
- an at job inherits your current environment when you submit it, unlike cron's minimal environment.
- OnBootSec and OnUnitActiveSec are monotonic and ignore the wall clock, so they suit boards with no reliable clock.
Reflect
- Explain to a colleague, in two sentences: build a systemd timer and service pair with OnCalendar and Persistent, and verify it with list-timers.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Official docssystemd.timer: timer unit configuration
- Official docssystemd.time: time and date specifications
Official documentation first. Links checked when the lesson was published.