Scheduling jobs to run unattended

Running things later: cron for anything repeating, at for a one-off, systemd timers as the modern equivalent, and the five fields whose order the exam expects you to know cold.

Lesson 2 of 3 in objective 107. Administrative tasks, part of LPIC-1 Exam 102-500.

The five time fields. Restrict both day fields and it runs when EITHER matches. */5 2 * * 1-5 /usr/bin/backup — part 1, */5: minute, 0-59. */5 is every fifth; part 2, 2: hour, 0-23; part 3, *: day of month, 1-31; part 4, *: month, 1-12; part 5, 1-5: day of week, 0-7 — 0 and 7 are both Sunday; part 6, /usr/bin/backup: the command. In /etc/crontab a USER comes first. 1 */5 2 2 3 * 4 * 5 1-5 6 /usr/bin/backup 1 minute, 0-59. */5 is every fifth 2 hour, 0-23 3 day of month, 1-31 4 month, 1-12 5 day of week, 0-7 — 0 and 7 are both Sunday 6 the command. In /etc/crontab a USER comes first
The five time fields. Restrict both day fields and it runs when EITHER matches.

The five fields

A crontab line is minute, hour, day of month, month, day of week, then the command. Minutes run 0 to 59, hours 0 to 23, day of month 1 to 31, month 1 to 12, and day of week 0 to 7 with both 0 and 7 meaning Sunday. An asterisk means every value, a comma lists them, a hyphen gives a range, and */5 in the minute field means every five minutes.

The rule that catches people: if BOTH day of month and day of week are restricted, the job runs when EITHER matches, not both. So 0 0 13 * 5 runs on the 13th and on every Friday, which is not what the person writing it usually meant.

What the two day fields together decide about which days a job runs. A grid of Day of month against Day of week. * (every date): * (every day) gives Runs daily; 5 (only Fridays) gives Runs on Fridays. 13 (a set date): * (every day) gives Runs on the 13th; 5 (only Fridays) gives Runs on the 13th OR Fridays. Day of week → Day of month ↓ * (every day) 5 (only Fridays) * (every date) Runs daily Runs on Fridays 13 (a set date) Runs on the 13th Runs on the 13th OR Fridays
What the two day fields together decide about which days a job runs.

Where crontabs live, and who may have one

A user's crontab is edited with crontab -e, listed with crontab -l and removed with crontab -r; the files themselves live under /var/spool/cron/ and are not meant to be edited directly. The SYSTEM crontab, /etc/crontab, and the files in /etc/cron.d/ have an extra field between the schedule and the command: the user to run as. Forgetting it is why a hand-written /etc/cron.d file silently fails.

The directories /etc/cron.hourly, cron.daily, cron.weekly and cron.monthly hold executable scripts run on that cadence with no schedule syntax at all. What actually runs them is run-parts, called once per directory from a single crontab entry, and it decides for itself which entries are eligible — which is the source of a classic silent failure. Debian's naming rule accepts only letters, digits, underscores and hyphens, so a script dropped in as backup.sh is skipped for the dot in its name while everything beside it runs, with nothing logged to say so. Packages drop the extension entirely for that reason. anacron exists for machines that are not on all the time: it tracks when a job last ran and catches up after a boot, which plain cron never does. Access is controlled by /etc/cron.allow and /etc/cron.deny — if allow exists, only the users in it may use cron; otherwise everyone except those in deny.

at runs a command once at a given time — at 22:00, at now + 1 hour — with atq listing pending jobs and atrm removing one; at.allow and at.deny work the same way as their cron counterparts. systemd timers are the newer mechanism: a .timer unit paired with a .service unit, listed with systemctl list-timers, and they can trigger relative to boot or to the last run in a way cron cannot express.

systemd-run is the way to get one of those without writing a unit file at all: it builds a TRANSIENT unit on the fly and starts it. With no timer option the command runs at once; with one, systemd-run creates a transient .timer as well and the command runs when it fires. So systemd-run --on-active=15m /usr/local/bin/report.sh is the answer on a systemd host with no atd installed and nothing to be installed. The timer options divide into monotonic ones — --on-active, --on-boot, --on-startup, --on-unit-active — which take a time SPAN such as 15m or 90s, and --on-calendar, which takes systemd calendar syntax such as an absolute date and time. Handing a span to --on-calendar is the mistake the distractors are built from.

A cron job runs with a minimal environment and its output is mailed to the owner. Redirecting to a file or to /dev/null is how that mail is silenced, and it is why a job that "does nothing" is often working perfectly and failing to find a command that is not in cron's PATH.

The places a cron job can live, and the two files that decide who may use cron. / contains /etc, /etc/crontab (an extra user field before the command), /etc/cron.d/ (drop-ins in the /etc/crontab format), /etc/cron.hourly/ (and daily, weekly, monthly: scripts, no schedule), /etc/cron.allow (if it exists, only these users may use cron), /etc/cron.deny (blocks its users; ignored if allow exists), /var/spool/cron/ (personal crontabs; edit with crontab -e, not by hand). / /etc /etc/crontab an extra user field before the command /etc/cron.d/ drop-ins in the /etc/crontab format /etc/cron.hourly/ and daily, weekly, monthly: scripts, no schedule /etc/cron.allow if it exists, only these users may use cron /etc/cron.deny blocks its users; ignored if allow exists /var/spool/cron/ personal crontabs; edit with crontab -e, not by hand
The places a cron job can live, and the two files that decide who may use cron.

Worth carrying in

min hour dom mon dow
The field order. Day of week 0 and 7 both mean Sunday.
*/5 * * * *
Every five minutes.
crontab -e / -l / -r
Edit, list and remove your own crontab.
/etc/crontab
System crontab: has an extra USER field before the command.
/etc/cron.d/
Drop-in system crontab files, same format as /etc/crontab.
/etc/cron.daily/
Executable scripts run daily; no schedule syntax.
anacron
Catches up jobs missed while the machine was off.
at 22:00 / atq / atrm
One-off scheduling, queue listing, and removal.
systemctl list-timers
systemd timer units and when they next fire.
systemd-run --on-active=15m
A transient unit and timer, no unit file. --on-calendar takes a date, not a span.
run-parts
Runs the scripts in /etc/cron.daily/ and friends. Skips names containing a dot, silently.
cron.allow / cron.deny
If allow exists it is the whitelist; otherwise deny is the blacklist.

What the exam does with this

Objective
107. Administrative tasks
Share of the exam
20% (the whole objective)
Questions in this lesson
20
Signed for by a person
0

Partly checked. None of the 20 questions here has been read against the cited source by a person. 20 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Scheduling jobs to run unattended

Questions in this lesson

Practise Scheduling jobs to run unattended

The rest of objective 107