Scheduling Tasks
Scheduling options
- Systemd timers are the primary solution for scheduling recurring jobs
crondis an older scheduling solution which is still supported and a bit easier to schedule custom tasksatis available to schedule non-recurring user tasks
Systemd timers
- Systemd provides unit.timer files that go together with unit.service files
- When using systemd timers, the timer should be enabled/started, NOT the service unit
- Systemd timers are often installed from RPM packages
- In the timer unit file, the
OnCalendaroption specifies when the service should be started - Systemd timers have been the default way for scheduling recurring services since RHEL 9
Timer activation
- Use
OnCalendarto schedule a recurring jobs (it’s not the same syntax as cron, very unfortunately) - Use
OnUnitActivateSecto start the unit a specific time after the unit was last activated - Use
OnBootSecorOnStartupSecto start the unit a specific time after booting - Read
man 7 systemd.timefor specification of the time format to be used
cron
- Cron is an old UNIX scheduling option
- It uses
crond, a daemon that checks its configuration to run cron jobs periodically - Still on RHEL 10,
crondis enabled as a systemd service by default - The
crondprocess checks its configuration every minute /etc/crontabis the main (managed) configuration file/etc/cron.dcan be used for drop-in files- User-specific cron jobs can be created using
crontab -e - The
/etc/crontabfile has a nice syntax example /etc/cron.{hourly,daily,weekly,monthly}can be used as a drop-in for scripts that need to be scheduled on a regular basisanacrontakes care of these jobs- They are executed on a regular basis, but not at a specific time
- Configuration is in
/etc/anacrontab
It’s important to note that user cron jobs run even when the user is logged out, but user level systemd timers do not by default. When a user needs to schedule jobs when they aren’t logged in,
cronoratmay be the best option
at
- The atd service must be running to run once-only jobs using
at - Use
at {time}to schedule a job- Type one or more job specs in the interactive shell
- Use Ctrl-D to close the shell
- Use
atqfor a list of jobs currently scheduled - Use
atrmto remove jobs from the list
Temporary files
-
In the past, temporary files were created in the
/tmpdirectory -
Without management, these files could stay around for a long time
-
As a solution, the
/tmpdirectory could be created on a RAM drive -
Nowadays,
systemd-tmpfilesis started while booting and manages temp files and directories -
Functionality has been expanded to manage other files also
-
It will create and delete tmp file automatically, according to the configuration files in the following locations:
/usr/lib/tmpfiles.d/etc/tmpfiles.d/run/tmpfiles.d
-
systemd-tmpfilesworks with related services to manage temporary files -
systemd-tmpfiles-setup.servicecreates and removes temp files according to the configuration -
systemd-tmpfiles-clean.timercalls thesystemd-tmpfiles-clean.serviceto remove temp files- By default 15 minutes after booting and also daily
-
d /run/myfiles 0750 root rootwill create the directory /run/myfiles if necessary. No action if it already exists -
D /run/myfiles 0750 root root 1dwill create the directory /run/myfiles and wipe its contents if it already exists. Files older than 1 day are eligible for automatic removal the next timesystemd-tmpfilesruns -
man tmpfiles.dprovides detailed information and examples
Lab Exercise
- Ensure the systemd timer that cleans up tmp files is enabled
- Create a /tmp-fast directory for all users and ensure that files older than 5 days are removed
- Create a /tmp-very-fast directory for all users and ensure that all files will be removed every day and on every boot
- Run a cron job that will issue the command
touch /tmp/cronfile2 minutes from now as user mac - Use
atto schedule a job to reboot your system 5 minutes from now after you ensure mac’s cron job ran
Solution
systemctl status systemd-tmpfiles-clean- /etc/tmpfiles.d/tmp-fast.conf
d /tmp-fast 1777 root root 5d - /etc/tmpfiles.d/tmp-very-fast.conf
D /tmp-very-fast 1777 root root 1d crontab -e, local time is Fri 2025-11-21 15:00 MST02 15 21 11 fri /bin/touch /tmp/cronfile- The /etc/crontab file specifies a user. That is because this is the system crontab file. When a user edits their crontab file, no user is specified. Crond will interpret a user-name as a command in a user’s crontab!
- The system’s local time is used, not UTC
- —
at 15:05 > systemctl reboot
- Clint Jordan