RH134 Ch 2: Scheduling with at, cron and systemd Timers
Linux gives you three ways to run work later. Pick by how often the job must run.
One-off jobs: at. at runs commands once at a given time; the atd service must be running. Times can be now + 2 hours, 16:30, noon tomorrow or teatime (16:00). Commands come from standard input:
bash echo 'date ~/at-test.txt' | at teatime tomorrow atq list jobs (root sees everyone's) at -c 3 show what job 3 will run atrm 3 cancel it
Repeating jobs: cron. Each user has a crontab, edited with crontab -e (root can use crontab -e -u student) and listed with crontab -l. The five time fields are minute, hour, day of month, month and day of week:
30 9 1-5 df -h / /home/student/disk.log 09:30 Mon–Fri /15 /usr/local/bin/check.sh every 15 minutes
System-wide jobs go in files under /etc/cron.d/, which have an extra user field after the time: /15 root find /tmp -name '.tmp' -delete. There are also /etc/cron.{hourly,daily,weekly,monthly} directories for scripts.
systemd timers. A timer unit starts a service unit on a schedule and logs to the journal like any service. You need two files with the same name: report.service (Type=oneshot, ExecStart=…) and report.timer with [Timer] OnCalendar=-- 02:00:00 and [Install] WantedBy=timers.target. Persistent=true runs a missed job after the machine was off. Then:
bash systemctl daemon-reload systemctl enable --now report.timer systemctl list-timers systemd-analyze calendar 'Mon..Fri 09:30' check a schedule
Temporary files. systemd-tmpfiles creates and cleans directories from rules in /etc/tmpfiles.d/.conf, for example d /run/webasha 0750 root root 1d (create the directory; delete contents older than a day). Apply now with systemd-tmpfiles --create.
Common exam mistakes - Putting a job in root's crontab when the task said a user's. - Leaving out the user field in /etc/cron.d files, or adding one in a personal crontab. - Enabling the .service instead of the .timer, or forgetting daemon-reload.