Scheduling Tasks
Theory Questions
1. Identify the default solution for scheduling recurring jobs in RHEL 9/10.
systemctl list-timers --all
crontab -l
- Why:
systemd timers are the modern default for system service scheduling, while cron remains the standard for user-defined recurring scripts.
- Gotchas: Assuming
cron is the sole default; systemd timers are preferred for system-level background services in modern RHEL distributions.
- Verify:
systemctl list-timers --all | head
echo -e "[Timer]\nOnCalendar=Sun *-*-* 11:00:00" > /etc/systemd/system/myjob.timer
- Why:
OnCalendar= defines absolute calendar events using day-of-week and timestamp syntax.
- Gotchas: Forgetting to run
systemctl daemon-reload and systemctl enable --now myjob.timer after creating or updating unit files.
- Verify:
systemctl list-timers myjob.timer
3. Start a timer a fixed duration after a systemd service starts.
- Command(s):
Add the following directive to the
[Timer] section of the unit file:
OnUnitActiveSec=5min
- Why:
OnUnitActiveSec sets a monotonic timer relative to when the associated service unit was last activated.
- Gotchas: Confusing
OnUnitActiveSec with OnBootSec (relative to system boot) or OnActiveSec (relative to timer unit activation).
- Verify:
systemctl show myjob.timer -p TimersMonotonic
4. Start a systemd user unit a fixed duration after user login.
- Command(s):
Add the following directive to
~/.config/systemd/user/myjob.timer:
OnActiveSec=5min
- Why:
OnActiveSec measures time relative to when the timer unit itself is activated, which occurs automatically at user login for user units.
- Gotchas: Forgetting to pass the
--user flag when managing user units (systemctl --user daemon-reload).
- Verify:
systemctl --user list-timers myjob.timer
5. Schedule a cron task to run on a specific day and time (e.g., Sunday at 11 a.m.).
(crontab -l 2>/dev/null; echo "0 11 * * 0 /path/to/script") | crontab -
- Why: The syntax
0 11 * * 0 translates to minute 0, hour 11, any day of month, any month, day-of-week 0 (Sunday).
- Gotchas: Missing one of the 5 time fields or using
7 for Sunday (while 7 is valid in some implementations, 0 is standard POSIX).
- Verify:
crontab -l
6. Schedule a cron job to run every 5 minutes on weekdays only.
(crontab -l 2>/dev/null; echo "*/5 * * * 1-5 /path/to/script") | crontab -
- Why:
*/5 evaluates to every 5 minutes; 1-5 restricts execution to Monday through Friday.
- Gotchas: Using
* for the day-of-week field, which would cause the job to execute on weekends.
- Verify:
crontab -l
7. Create a cron job for a specific user.
crontab -u username -e
- Why: The
-u flag specifies the target user profile whose spool file is being edited.
- Gotchas: Attempting to run
crontab -u without root administrative privileges.
- Verify:
crontab -u username -l
8. Identify recommended and non-recommended ways to specify cron jobs.
ls -l /etc/cron.d/ # Recommended for system packages/scripts
ls -l /var/spool/cron/ # Non-recommended (manual direct file edits)
- Why: Using
/etc/cron.d/ or standard crontab -e ensures proper validation, while editing /var/spool/cron/ files directly bypasses syntax validation and can corrupt job definitions.
- Gotchas: Manually modifying
/etc/crontab directly instead of placing modular drop-in files into /etc/cron.d/.
- Verify:
cat /etc/cron.d/example_job
9. Close the at shell after entering commands.
- Command(s):
Press
Ctrl+D (EOF) on an empty line.
- Why:
Ctrl+D sends the End-Of-File (EOF) signal to standard input, instructing at to finalize and schedule the job sequence.
- Gotchas: Pressing
Ctrl+C, which cancels the input session without saving the scheduled job.
- Verify:
atq
10. View current at jobs scheduled for execution.
atq
- Why:
atq queries the atd daemon spool and displays all pending execution queues.
- Gotchas: Running
crontab -l, which only lists scheduled cron jobs, not pending one-time at tasks.
- Verify:
atq
(crontab -l 2>/dev/null; echo '0 0 * * 1 [ $(( $(date +\%V) % 2 )) -eq 1 ] && /path/to/script') | crontab -
- Why: Cron does not natively support bi-weekly intervals; using the shell to verify if the ISO week number (
%V) is odd ensures execution occurs every alternate week.
- Gotchas: Forgetting to escape
% signs (\%) inside standard crontab entries, as unescaped % symbols are interpreted as newlines by cron.
- Verify:
crontab -l
12. Start a service 5 minutes after system boot using a systemd timer.
- Command(s):
Add the following directive to the
[Timer] section:
OnBootSec=5min
- Why:
OnBootSec configures a timer relative to the kernel launch timestamp.
- Gotchas: Relying on
OnCalendar, which specifies wall-clock time and will not guarantee a precise delay if the machine was powered off.
- Verify:
systemctl show myjob.timer -p TimersMonotonic
13. Troubleshoot a systemd timer that is not triggering its service.
systemctl status myjob.timer
journalctl -u myjob.timer
- Why: Checking
systemctl status reveals whether the timer unit is active, while journalctl captures execution failure logs or missed triggers.
- Gotchas: Inspecting only the target service unit (
myjob.service) while overlooking state issues with the associated myjob.timer unit.
- Verify:
systemctl list-timers --all | grep myjob
14. Start a service every 7 hours using the simplest method.
- Command(s):
Add the following directive to the
[Timer] section:
OnUnitActiveSec=7h
- Why: Monotonic timers handle non-standard repeating intervals (such as 7-hour cycles) far cleaner than complex
OnCalendar expressions.
- Gotchas: Attempting
OnCalendar=*-*-* 00/7:00:00, which creates uneven distribution patterns across midnight boundaries.
- Verify:
systemctl show myjob.timer -p TimersMonotonic
15. Match a specific timer unit to a specific service unit.
- Command(s):
Name both files identically (e.g.,
backup.timer and backup.service), or explicitly define the target in the timer file:
[Timer]
Unit=custom-name.service
- Why:
systemd automatically links name.timer to name.service by default; the Unit= key overrides this behavior for custom named targets.
- Gotchas: Assuming the timer unit name must match the target service name even when
Unit= is explicitly configured.
- Verify:
systemctl cat backup.timer | grep Unit
16. Schedule a cron job for another user.
crontab -u targetuser -e
- Why: The
-u flag permits root users to inspect or modify the crontab configuration of any system user.
- Gotchas: Executing the command as an unprivileged user, which triggers a permission error.
- Verify:
crontab -u targetuser -l
17. Block a specific user from scheduling cron jobs.
echo "username" >> /etc/cron.deny
- Why: Placing an explicit username into
/etc/cron.deny revokes access to the crontab binary for that account.
- Gotchas: Failing to account for
/etc/cron.allow; if /etc/cron.allow exists, system access is restricted exclusively to listed users, making /etc/cron.deny ignored.
- Verify:
su - username -c "crontab -l"
18. Ensure a job runs every day even if the server was down at execution time.
- Command(s):
Add the following directive to the
[Timer] section of the systemd timer:
Persistent=true
- Why:
Persistent=true instructs systemd to store the last trigger timestamp on disk and execute missed tasks immediately upon system reboot.
- Gotchas: Expecting standard
cron to catch up on missed tasks without utilizing anacron or systemd persistent timers.
- Verify:
systemctl cat myjob.timer | grep Persistent
19. Identify the service required for at job scheduling.
systemctl status atd
- Why: The
atd daemon processes and manages the execution queue for single-instance delayed jobs created via at.
- Gotchas: Confusing
atd with crond, which strictly handles recurring cron schedules.
- Verify:
systemctl is-active atd
20. Check whether any at jobs are currently scheduled.
atq
- Why:
atq queries the global execution spool and lists all pending jobs for the calling user (or all users when run as root).
- Gotchas: Running
atq as a standard user expecting to see jobs queued by other accounts.
- Verify:
atq
Lab Tasks
Task Scheduling Practice
Task 1: Create a cron job that updates all software on your computer every evening at 11 p.m.
echo "0 23 * * * root dnf update -y" > /etc/cron.d/system-update
chmod 644 /etc/cron.d/system-update
- Why: Drop-in files under
/etc/cron.d/ require a system execution context (the root user field) between the time fields and command. Setting standard permissions (644) ensures security compliance.
- Gotchas: Omitting the user field (
root) in /etc/cron.d/ drop-ins, which causes execution failures.
- Verify:
cat /etc/cron.d/system-update
Task 2: Schedule the machine to reboot at 3 a.m. tomorrow morning using at.
at 03:00 tomorrow
shutdown -r now
# Press Ctrl+D to save and exit
- Why:
at 03:00 tomorrow opens an interactive prompt to specify commands for delayed execution; shutdown -r now initiates a clean system reboot.
- Gotchas: Failing to issue
Ctrl+D to save the queued job commands.
- Verify:
atq
Task 3: Use a systemd timer to start the vsftpd service five minutes after system boot.
systemctl disable --now vsftpd.service
cat << 'EOF' > /etc/systemd/system/vsftpd-delay.timer
[Unit]
Description=Delay vsftpd service start by 5 minutes
[Timer]
OnBootSec=5min
Unit=vsftpd.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now vsftpd-delay.timer
- Why: Disabling
vsftpd.service ensures it does not launch automatically at boot via default target dependencies, delegating startup control entirely to OnBootSec=5min in vsftpd-delay.timer.
- Gotchas: Leaving
vsftpd.service enabled, which causes it to start immediately at boot regardless of timer settings.
- Verify:
systemctl list-timers vsftpd-delay.timer