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
Choose the right scheduler
Pick the tool by the kind of task.
| Tool | For | Example |
|---|---|---|
| systemd timer | Repeating tasks, with logs in the journal and dependencies | Clean temp files every day |
| cron | Repeating tasks, simple one-line format | Backup every night at 02:00 |
| anacron | Daily/weekly jobs on machines that are not always on | Laptop: run once a day when it is on |
| at | One task, once, at a later time | Restart a service tonight at 23:00 |
Use systemd timers
A timer unit starts a service unit with the same name at the times you set. Logs go to the journal.
- 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 - 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 - Enable the timer (not the service) and check it.
| Timer setting | Meaning |
|---|---|
OnCalendar=daily | Every day at 00:00 (also hourly, weekly, Mon..Fri 08:00) |
OnBootSec=15min | 15 minutes after boot |
OnUnitActiveSec=1h | 1 hour after the last run |
Persistent=true | Run missed jobs after the machine was off |
RandomizedDelaySec=10m | Start up to 10 minutes later at random |
Onthoud
Test a time expression with systemd-analyze calendar 'Mon..Fri 08:00'.
Manage the cron service
The crond daemon checks every minute if a job must run. It is installed and enabled on RHEL by default.
Read the cron time format
A cron line has 5 time fields, then the command.
# min hour day-of-month month day-of-week command
30 2 * * * /usr/local/bin/backup.sh| Field | Values |
|---|---|
| Minute | 0–59 |
| Hour | 0–23 |
| Day of month | 1–31 |
| Month | 1–12 or jan–dec |
| Day of week | 0–7 (0 and 7 = Sunday) or sun–sat |
| Example | Runs |
|---|---|
0 * * * * | Every hour at minute 0 |
*/15 * * * * | Every 15 minutes |
0 8-17 * * 1-5 | Every hour from 8 to 17, Monday to Friday |
0 0 1 * * | The first day of every month at midnight |
30 23 * * 0 | Sunday at 23:30 |
@reboot | Once at every boot |
Let op
When you fill in both day of month and day of week, cron runs when either matches, not both.
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/.
- Edit your own crontab.
crontab -e - Add a line, save and leave the editor.crontab -e
*/10 * * * * /usr/bin/df -h / >> /var/tmp/disk.log - List it, and as root manage the jobs of another user.
- 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
| Place | Who writes it | User field |
|---|---|---|
crontab -e (saved in /var/spool/cron/USER) | Each user | No |
/etc/crontab, /etc/cron.d/* | root, packages | Yes |
/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.
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.
#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.monthlyNotitie
To run a script daily in a simple way, put it (executable, without a dot in the name) in /etc/cron.daily/.
Control who may use cron
Two files decide which users may use crontab: /etc/cron.allow and /etc/cron.deny.
| Situation | Result |
|---|---|
cron.allow exists | Only users listed in it (and root) |
Only cron.deny exists | Everyone except the users in it (RHEL default: empty deny file) |
at uses the same idea with /etc/at.allow and /etc/at.deny.
Run a task once with at
at TIME reads commands and runs them once at that time. The atd service must run.
- Make sure atd runs.
- 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 - Plan with a pipe, list and remove jobs.
| Time form | Example |
|---|---|
| Clock time | at 16:30, at 4pm |
| Relative | at now + 30 minutes, at now + 2 days |
| Date | at 09:00 2026-12-31, at noon tomorrow |
batch runs a job when the system load is low (under 0.8 by default).
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 -eedits your own jobs;crontab -e -u USERedits jobs of another user (as root).- System cron jobs go in
/etc/crontab,/etc/cron.d/and the/etc/cron.hourly|daily|weekly|monthlyfolders. - anacron runs daily, weekly and monthly jobs that were missed while the system was off.
/etc/cron.allowand/etc/cron.deny(and the same for at) control who may plan jobs.at 14:00runs commands once;atqlists andatrmremoves jobs.- A timer unit (
NAME.timer) starts a service unit withOnCalendar=orOnBootSec=.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| cron / crond | The service that runs planned jobs. |
| crontab | The table of cron jobs of one user. |
| Cron fields | The 5 time fields of a cron line. |
| anacron | Runs periodic jobs that were missed. |
| at | Runs a job one time at a set time. |
| Timer unit | A systemd unit that starts a service on a schedule. |
| OnCalendar | The time rule in a timer, like *-*-* 02:00:00. |
| cron.allow / cron.deny | Lists of users who may / may not use cron. |
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?
atq.
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).