Linux › Section G · System configuration › Topic G.5

Recurring tasks with cron and timer units

Backups, log rotation, certificate renewal and clean-up jobs all need to run on a schedule without anyone present. Linux offers two mature tools for this, the classic cron daemon and systemd timer units, and you should be able to read and write both.

Two schedulers

cron is a daemon (cron on Debian-family systems, cronie on Fedora and RHEL) that wakes every minute, checks its tables and starts any command whose time has come. systemd timer units do the same job inside systemd: a .timer unit decides when, and a matching .service unit holds what to run. cron is small, universal and easy to read; timers integrate with the journal, dependencies and resource limits. Both appear on real systems, often side by side, because a distribution may use cron for some packages and timers for others.

The five fields of a crontab line

A crontab line has five time fields and then the command: minute (0-59), hour (0-23), day of month (1-31), month (1-12 or names), day of week (0-7 or names, where 0 and 7 are both Sunday). An asterisk means every value. A hyphen gives a range, a comma a list, and a slash a step: */15 means every 15th value, and 9-17 is hours nine to seventeen inclusive. Steps apply within one field only, so */35 in the minute field runs at minutes 0 and 35 of every hour, not every 35 minutes. Shortcuts replace the five fields: @hourly, @daily, @weekly, @monthly, @yearly and @reboot.

The day-of-month and day-of-week trap

When both the day-of-month and day-of-week fields are restricted, that is, neither is an asterisk, cron runs the job when either one matches. So 30 4 1,15 * 5 runs at 04:30 on the 1st and the 15th and also every Friday; it does not mean only Fridays that fall on the 1st or 15th. If you want the stricter meaning, put the rule in the command itself, for example by testing the date with a shell condition. When only one of the two fields is restricted, the other is an asterisk and the rule is simple.

Managing crontabs

Each user has a personal crontab. crontab -e edits it, crontab -l lists it, and crontab -r removes all of it with no confirmation (crontab -i asks first). As root, crontab -u ana -l inspects another user's table. System-wide tables are /etc/crontab and the files in /etc/cron.d/; these have a sixth field naming the user, as in 30 2 * * * root /usr/local/bin/backup.sh. The directories /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly hold executable scripts run at those cadences by a helper program. cron starts jobs with a minimal environment: SHELL is /bin/sh, PATH is short, and the working directory is the owner's home. Use full paths.

Worked example: when does each line run next?

Take the moment Friday 6 November 2026, 10:20, and compute with a small python script that applies the rules above. 1. 30 2 * * * is daily at 02:30: next Saturday 7 November 02:30. 2. */15 9-17 * * 1-5 is every 15 minutes from 09:00 to 17:45 on weekdays: next today at 10:30, then 10:45. 3. 0 9 1,15 * 1 restricts both day fields, so the either-match rule applies: the next run is Monday 9 November 09:00, then Sunday 15 November. 4. 0 0 31 * * needs a 31st, so months with 30 days are skipped: next Thursday 31 December 00:00. 5. Check line 2: nine hours times four runs gives 36 runs on a weekday.

Output, mail and the percent sign

cron mails a job's output to the crontab owner when a mail system exists, or discards it. Set MAILTO=ops@example.com at the top to redirect it, or MAILTO= to silence mail, and redirect output yourself: >> /var/log/backup.log 2>&1. A literal percent sign in the command line is special: an unescaped % becomes a newline and the text after it is fed to the command as input. In date +%F you must therefore write date +\%F. CRON_TZ sets a time zone for a table on cron versions that support it. A job that outlasts its interval can overlap with the next run; flock -n /run/backup.lock command prevents that.

Timer units

A timer named report-backup.timer starts report-backup.service unless Unit= says otherwise. The [Timer] section holds the schedule. Realtime timers use OnCalendar=, a wall-clock expression. Monotonic timers count from an event: OnBootSec= from boot, OnActiveSec= from the timer's own activation, OnUnitActiveSec= from the last time the service ran. Persistent=true, valid with OnCalendar=, records the last run on disk and runs the service at once if a scheduled time was missed while the machine was off. AccuracySec= defaults to one minute, so a run may start up to a minute late to save power. RandomizedDelaySec= spreads many machines across a window.

OnCalendar syntax

The form is DayOfWeek Year-Month-Day Hour:Minute:Second, with every part optional. An omitted time means 00:00:00 and an omitted date means every day. A star matches anything, commas list values, .. gives a range and a slash gives a step. Mon..Fri *-*-* 03:00:00 means 03:00 on weekdays. *:0/15 means every 15 minutes, on the quarter hours. Shorthands include hourly, daily, weekly (Monday at 00:00), monthly and yearly. systemd-analyze calendar 'Mon..Fri 03:00' normalises an expression and shows the next elapse time, so you can check a schedule before committing to it. Times use the system's local time zone unless one is named.

Worked example: converting a cron job to a timer

Convert 0 3 * * 1-5 /usr/local/bin/backup.sh. 1. Write /etc/systemd/system/backup.service containing [Unit] Description=Weekday backup, then [Service] Type=oneshot and ExecStart=/usr/local/bin/backup.sh. 2. Write /etc/systemd/system/backup.timer containing [Timer] OnCalendar=Mon..Fri *-*-* 03:00:00 and Persistent=true, then [Install] WantedBy=timers.target. 3. Check the schedule: systemd-analyze calendar 'Mon..Fri *-*-* 03:00:00'. 4. Load and start: sudo systemctl daemon-reload, then sudo systemctl enable --now backup.timer. Enable the timer, not the service. 5. Inspect: systemctl list-timers shows the next run, Monday 9 November 2026 at 03:00 from our example moment, and journalctl -u backup.service shows each result.

Choosing between them

Timers give you per-run logs in the journal, dependency ordering (run after the network is up), resource limits through cgroups, catch-up with Persistent=true, and random delays. cron gives you a single line, an interface every administrator knows, and availability on systems without systemd. Plain cron has no catch-up for runs missed while the machine was off; the anacron tool fills that gap for daily, weekly and monthly jobs. For new work on a systemd machine, timers are usually the better fit when you need logging or reliability; for a quick personal job, a crontab line is fine.

Common mistakes to avoid

First, relying on your login PATH inside cron; use absolute paths or set PATH. Second, forgetting the user field in /etc/crontab and /etc/cron.d, or adding one in a personal crontab. Third, misreading the either-match rule for day fields. Fourth, an unescaped % in a command. Fifth, running crontab -r when you meant -e. Sixth, enabling the .service instead of the .timer, or forgetting daemon-reload after writing unit files. Seventh, forgetting that OnCalendar uses local time, which can surprise you around daylight-saving changes.

Key takeaways

A crontab line is minute, hour, day of month, month, day of week, then the command; system tables add a user field. When both day fields are restricted, either may match. cron's environment is minimal and a bare percent sign is special. A timer unit pairs a .timer with a .service; OnCalendar= is wall-clock, OnBootSec= and OnUnitActiveSec= are monotonic, and Persistent=true catches up missed runs. Verify schedules with systemd-analyze calendar and inspect them with systemctl list-timers.

Flashcards

Flashcard

crontab fields

minute hour day-of-month month day-of-week

Then the command.

Test your understanding

Question 1 of 5

Using the either-match rule, when does 0 9 1,15 * 1 next run after Friday 6 November 2026, 10:20?

Question 2 of 5

How many times a day does */15 9-17 * * 1-5 run on a weekday?

Question 3 of 5

Which line is correct in /etc/crontab to run a backup at 02:30 daily as root?

Question 4 of 5

A server is powered off at 03:00, when a daily timer was due, and switched on at 08:00. Which setting makes the job run at start-up?

Question 5 of 5

Which OnCalendar expression matches the cron schedule 0 3 * * 1-5?

← Previous topicNext topic →