Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 12Naam in het boek: Scheduling Tasks

Run tasks on a schedule

Some work must run at fixed times (backups, reports) or once later (a reboot tonight). RHEL has three tools for this: systemd timers, cron and at. This chapter shows all three, plus anacron and how to control who may use them.

  • 11 Lessen
  • 9 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Taken plannen met at, cron en systemd-timers

12.1

Choose the right scheduler

Pick the tool by the kind of task.

ToolForExample
systemd timerRepeating tasks, with logs in the journal and dependenciesClean temp files every day
cronRepeating tasks, simple one-line formatBackup every night at 02:00
anacronDaily/weekly jobs on machines that are not always onLaptop: run once a day when it is on
atOne task, once, at a later timeRestart a service tonight at 23:00

12.2

Use systemd timers

A timer unit starts a service unit with the same name at the times you set. Logs go to the journal.

systemctl list-timers --all
systemctl cat fstrim.timer
systemctl cat logrotate.timer
  1. Create the service that does the work.
    /etc/systemd/system/backup-etc.service
    [Unit]
    Description=Back up /etc
     
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/tar -czf /var/backup/etc-%H.tgz /etc
  2. Create the timer with the same name.
    /etc/systemd/system/backup-etc.timer
    [Unit]
    Description=Back up /etc every day at 02:30
     
    [Timer]
    OnCalendar=*-*-* 02:30:00
    Persistent=true
     
    [Install]
    WantedBy=timers.target
  3. Enable the timer (not the service) and check it.
    mkdir -p /var/backup
    systemctl daemon-reload
    systemctl enable --now backup-etc.timer
    systemctl list-timers backup-etc.timer
Timer settingMeaning
OnCalendar=dailyEvery day at 00:00 (also hourly, weekly, Mon..Fri 08:00)
OnBootSec=15min15 minutes after boot
OnUnitActiveSec=1h1 hour after the last run
Persistent=trueRun missed jobs after the machine was off
RandomizedDelaySec=10mStart up to 10 minutes later at random

Onthoud

Test a time expression with systemd-analyze calendar 'Mon..Fri 08:00'.

12.3

Manage the cron service

The crond daemon checks every minute if a job must run. It is installed and enabled on RHEL by default.

systemctl status crond
ls /etc/cron.d/ /etc/cron.daily/ /var/spool/cron/

12.4

Read the cron time format

A cron line has 5 time fields, then the command.

crontab line
# min  hour  day-of-month  month  day-of-week   command
  30   2     *             *      *             /usr/local/bin/backup.sh
FieldValues
Minute0–59
Hour0–23
Day of month1–31
Month1–12 or jan–dec
Day of week0–7 (0 and 7 = Sunday) or sun–sat
ExampleRuns
0 * * * *Every hour at minute 0
*/15 * * * *Every 15 minutes
0 8-17 * * 1-5Every hour from 8 to 17, Monday to Friday
0 0 1 * *The first day of every month at midnight
30 23 * * 0Sunday at 23:30
@rebootOnce at every boot

Let op

When you fill in both day of month and day of week, cron runs when either matches, not both.

12.5

Create cron jobs

Users edit their own job list with crontab -e. root can edit the list of any user. System jobs go in files in /etc/cron.d/.

  1. Edit your own crontab.
    crontab -e
  2. Add a line, save and leave the editor.
    crontab -e
    */10 * * * * /usr/bin/df -h / >> /var/tmp/disk.log
  3. List it, and as root manage the jobs of another user.
    crontab -l
    crontab -e -u lisa
    crontab -l -u lisa
  4. For system jobs, use a file in /etc/cron.d with an extra user field.
    /etc/cron.d/cleanup
    0 3 * * * root find /var/tmp -type f -mtime +7 -delete
PlaceWho writes itUser field
crontab -e (saved in /var/spool/cron/USER)Each userNo
/etc/crontab, /etc/cron.d/*root, packagesYes
/etc/cron.hourly/, daily/, weekly/, monthly/Scripts that run at that interval—

Examentip

A task like "user X must run Y every day at 14:23" = crontab -e -u X and the line 23 14 * * * Y. Check with crontab -l -u X.

Op het werk

cron jobs run with a very small environment (short PATH, no aliases). Use full paths and send output to a log, or it goes to local mail.

12.6

Understand anacron

anacron runs the daily, weekly and monthly jobs even when the machine was off at the planned time. On RHEL, cron starts anacron every hour; anacron runs what is due.

cat /etc/anacrontab
ls /var/spool/anacron/
/etc/anacrontab (part)
#period in days   delay in minutes   job-identifier   command
1                 5                  cron.daily       nice run-parts /etc/cron.daily
7                 25                 cron.weekly      nice run-parts /etc/cron.weekly
@monthly          45                 cron.monthly     nice run-parts /etc/cron.monthly

Notitie

To run a script daily in a simple way, put it (executable, without a dot in the name) in /etc/cron.daily/.

Commando's in deze les

12.7

Control who may use cron

Two files decide which users may use crontab: /etc/cron.allow and /etc/cron.deny.

SituationResult
cron.allow existsOnly users listed in it (and root)
Only cron.deny existsEveryone except the users in it (RHEL default: empty deny file)
echo lisa >> /etc/cron.deny
crontab -e
You (lisa) are not allowed to use this program (crontab)

at uses the same idea with /etc/at.allow and /etc/at.deny.

12.8

Run a task once with at

at TIME reads commands and runs them once at that time. The atd service must run.

  1. Make sure atd runs.
    dnf install -y at
    systemctl enable --now atd
  2. Plan a task: type the commands, then press Ctrl+D.
    at 23:00
    at> systemctl restart httpd
    at> <EOT>
    job 3 at Sun Oct 25 23:00:00 2026
  3. Plan with a pipe, list and remove jobs.
    echo "tar -czf /tmp/home.tgz /home/lisa" | at now + 2 hours
    atq
    at -c 3
    atrm 3

batch runs a job when the system load is low (under 0.8 by default).

12.9

Remember the main points

The most important facts of this chapter, in short.

  • RHEL has three ways to plan jobs: systemd timers, cron (repeating jobs) and at (one time).
  • A cron line has 5 time fields: minute, hour, day of month, month, day of week — then the command.
  • crontab -e edits your own jobs; crontab -e -u USER edits jobs of another user (as root).
  • System cron jobs go in /etc/crontab, /etc/cron.d/ and the /etc/cron.hourly|daily|weekly|monthly folders.
  • anacron runs daily, weekly and monthly jobs that were missed while the system was off.
  • /etc/cron.allow and /etc/cron.deny (and the same for at) control who may plan jobs.
  • at 14:00 runs commands once; atq lists and atrm removes jobs.
  • A timer unit (NAME.timer) starts a service unit with OnCalendar= or OnBootSec=.

12.10

Learn the key terms

The words of this chapter in simple English.

TermMeaning
cron / crondThe service that runs planned jobs.
crontabThe table of cron jobs of one user.
Cron fieldsThe 5 time fields of a cron line.
anacronRuns periodic jobs that were missed.
atRuns a job one time at a set time.
Timer unitA systemd unit that starts a service on a schedule.
OnCalendarThe time rule in a timer, like *-*-* 02:00:00.
cron.allow / cron.denyLists of users who may / may not use cron.

12.11

Check what you learned

Think about the answer first, then open it.

1Write the cron time for every weekday at 14:23.

23 14 * * 1-5.

2How do you plan a job for user anna as root?
3Which cron line runs a script every 15 minutes?

*/15 * * * * /path/script.sh.

4How do you run a command one time today at 18:00?

echo "COMMAND" | at 18:00 (or type at 18:00, then the command, then Ctrl+D).

5How do you see the at jobs that are waiting?
6What does anacron do for a laptop that was off at night?

It runs the missed daily, weekly or monthly jobs after the system starts.

7How do you see all timers and when they run next?
8How do you stop user bob from using cron?

Put bob in /etc/cron.deny (or use /etc/cron.allow with only the allowed users).

Oefenen

Alle commando's in dit hoofdstuk