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 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.
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.
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 extraUSERfield before the command. - /etc/cron.d/
- Drop-in system
crontabfiles, 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-calendartakes 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
- Day of month and day of week together are an OR, not an AND.
/etc/crontaband/etc/cron.dfiles carry a user field; a personalcrontabdoes not.cron.allowtakes precedence — when it exists,cron.denyis ignored entirely.- A script in
/etc/cron.daily/with a dot in its name is skipped byrun-partsand nothing says so.
- 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
- Which user crontab line runs /usr/local/bin/backup.sh at 03:30 every day? machine-checked
- A crontab line begins with the fields `*/10 8-17 * * 1-5`. When does the job run? machine-checked
- Select the TWO statements that correctly describe system crontabs (/etc/crontab and files under /etc/cron.d) compared with per-user crontabs. machine-checked
- You meant to edit your personal crontab but typed `crontab -r`. What happened? machine-checked
- As root, list the contents of the user bob's personal crontab without editing it. Type the complete command. machine-checked
- A single report must be generated once, at 22:00 tonight, and never again. Which approach fits best? machine-checked
- A laptop is powered off most nights, so the nightly maintenance jobs scheduled by plain cron never run. Which mechanism is designed to catch up such missed periodic jobs after the machine is next started? machine-checked
- On a systemd system you have created /etc/systemd/system/backup.timer containing a [Timer] section with `OnCalendar=*-*-* 03:30:00`. What else is required, and what does that OnCalendar value mean? machine-checked
- /etc/cron.allow exists on a host and contains only the line `alice`. /etc/cron.deny exists too and lists `bob`. Which users may run crontab? machine-checked
- On a Debian system you drop an executable script named backup.sh into /etc/cron.daily/. Days pass and it never runs, although other scripts in the same directory clearly do. What is the most likely reason? machine-checked
- A user crontab contains the entry `0 6 1 * 1 /usr/local/bin/report`. On which days does the job actually run? machine-checked
- A colleague queued a job with at before going on leave. You can see it as job 7 in the queue, but you need to know exactly which commands it will execute before deciding whether to keep it. Which command shows you that? machine-checked
- Type the single command, with no options and no arguments, that lists your own pending at jobs. machine-checked
- Every entry in root's crontab writes progress messages to standard output, so root's mailbox fills up with one message per run. You want the jobs to keep running and keep writing their output, but you want cron to stop mailing it. Which line, placed near the top of that crontab, achieves this? machine-checked
- Which crontab special string is equivalent to the time specification `0 0 * * 0`? machine-checked
- A workstation runs a systemd timer with `OnCalendar=*-*-* 02:00:00`, but staff shut the machine down each evening, so the 02:00 activation is missed every night. Which directive in the [Timer] section makes systemd run the missed activation as soon as the machine boots again? machine-checked
- A systemd host has no atd installed and no package installation is permitted. You must run /usr/local/bin/report.sh exactly once, fifteen minutes from now, without writing any unit files. Which command does it? machine-checked
- Select the THREE statements that are true about systemd timer units. machine-checked
- Type the complete command, including its option, that a user runs to open their own crontab in an editor so that cron installs the result when the editor exits. machine-checked
- Select the TWO true statements about /etc/at.allow and /etc/at.deny. machine-checked
Practise Scheduling jobs to run unattended
The rest of objective 107
- User and group accounts
- Scheduling jobs to run unattended — you are here
- Locales, character encodings and time zones