Red Hat Certified System Administrator(RHCSA)
Deploy Configure and Maintain Systems
Verify completion of scheduled jobs
Welcome to our detailed guide on verifying the completion of scheduled jobs on CentOS Stream 8. By default, this operating system logs all events generated by cron, anacron, and at. This guide will show you how to confirm that your scheduled jobs are executing correctly.
Note
CentOS Stream 8 logs provide detailed information including command output, which can be extremely useful for troubleshooting and ensuring that your tasks run as expected.
Verifying Cron Jobs
Begin by reviewing your user-specific crontab. To open your crontab for editing, run:
crontab -e
In this example, two commands have been added to the crontab, scheduled to run every minute:
* * * * * /bin/echo "Just testing cron"
* * * * * /bin/echo "Just testing cron again"
These commands output their respective messages every minute. After adding these jobs, wait at least one minute before checking the logs. To view cron log entries, execute:
sudo cat /var/log/cron
You should see log entries resembling the following:
Mar 24 01:06:01 LFCS-CentOS CROND[9265]: (aaron) CMD (/bin/echo "Just testing cron again")
Mar 24 01:07:02 LFCS-CentOS CROND[9347]: (aaron) CMD (/bin/echo "Just testing cron again")
...
These log entries display the execution time, command details, and output. If you wish to view only executed command logs, filter the logs using:
sudo grep CMD /var/log/cron
When you are finished testing or need to remove the current crontab settings, clear them with:
crontab -r
Reviewing the System-Wide Crontab
The system-wide crontab is a useful reference for understanding cron syntax and configuration. To view its content, run:
cat /etc/crontab
You will see content similar to this:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12) OR jan,feb,mar,apr ...
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# │ │ │ │ │
# * * * * * user-name command to be executed
Notice the MAILTO
setting, which directs the output of cron jobs to the specified user (in this example, root). This ensures that the results of cron jobs are mailed, providing an additional layer of monitoring.
Verifying Anacron Jobs
Anacron is designed to schedule jobs on daily, weekly, or monthly intervals and ensures that they are executed even if the system was off at the scheduled time. Inspect the anacrontab configuration with:
sudo vi /etc/anacrontab
You should see content similar to:
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
# 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
1 10 test_job /bin/echo "Testing anacron"
The MAILTO
directive ensures that the output for each job, such as test_job
, is sent to the specified email recipient. Since waiting a full day for testing isn’t practical, you can manually trigger anacron jobs with:
sudo anacron -n
To focus on anacron-specific log messages, filter the cron log with:
sudo grep anacron /var/log/cron
For example, you'll see log entries like:
Mar 24 01:15:27 LFCS-CentOS anacron[8903]: Job `test_job` started
Mar 24 01:15:27 LFCS-CentOS anacron[8903]: Job `test_job` terminated (produced output)
If you need logs exclusively for test_job
, further filter by:
sudo grep test_job /var/log/cron
Enhancing Test Job Logging for Anacron
To capture command output from an anacron job more clearly, modify the job command to pipe its output to systemd-cat
with a custom identifier. Edit /etc/anacrontab
:
sudo vi /etc/anacrontab
Update the test_job
line as follows:
1 10 test_job /bin/echo "Testing anacron" | systemd-cat --identifier=test_job
After saving your changes, force anacron to run all jobs irrespective of their previous status using:
sudo anacron -n -f
Then, view the system log to find the output:
journalctl -e
You should see entries similar to:
Mar 24 01:19:38 LFCS-CentOS anacron[9869]: Job `test_job' started
Mar 24 01:19:38 LFCS-CentOS test_job[9887]: Testing anacron
Mar 24 01:19:38 LFCS-CentOS anacron[9869]: Job `test_job' terminated
Once you finish testing, remember to remove or comment out the modified logging command if it is no longer required.
Verifying at Jobs
The at
command schedules one-time jobs. To schedule a job to run in one minute, enter:
at 'now + 1 minute'
After this command, you will be prompted to enter the command you wish to run. For example:
echo "My at job produced this output"
Press Control-D (Ctrl+D) to signal the end of input. The system will display a message like:
job 4 at Thu Mar 24 01:22:00 2022
To view the at job log entries:
sudo grep atd /var/log/cron
You should see log lines indicating the start of the job, for example:
Mar 24 01:04:00 LFCS-CentOS atd[9079]: Starting job 2 (a0000201a3224c) for user 'aaron' (1000)
Mar 24 01:07:00 LFCS-CentOS atd[9319]: Starting job 3 (a0000301a3224f) for user 'aaron' (1000)
Keep in mind that the default logging for at jobs does not capture the command output. To log the output, schedule an at job by piping its output to systemd-cat
:
at 'now + 1 minute'
Then, when prompted, enter:
echo "My at job produced this output" | systemd-cat --identifier=at_scheduled_backup
Once the job executes, review the log output with:
journalctl | grep at_scheduled_backup
Depending on your system settings, you might also find relevant messages in /var/log/messages
or /var/log/syslog
.
Conclusion
This guide has detailed how to verify the successful execution of scheduled jobs using cron, anacron, and at on CentOS Stream 8. By examining log files and, when necessary, modifying your job commands to capture their output, you can ensure that your scheduled processes are running correctly and troubleshoot issues as they arise.
For further queries and comprehensive documentation on cron, anacron, or at scheduling, you might want to visit the CentOS Documentation.
Thank you for reading, and please proceed to the next section for additional demonstrations and related topics.
Watch Video
Watch video content
Practice Lab
Practice lab