This guide explains how to verify the completion of scheduled jobs on CentOS Stream 8 using cron, anacron, and at commands.
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.
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.
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:
Copy
Ask AI
sudo cat /var/log/cron
You should see log entries resembling the following:
The system-wide crontab is a useful reference for understanding cron syntax and configuration. To view its content, run:
Copy
Ask AI
cat /etc/crontab
You will see content similar to this:
Copy
Ask AI
SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=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.
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:
Copy
Ask AI
sudo vi /etc/anacrontab
You should see content similar to:
Copy
Ask AI
# /etc/anacrontab: configuration file for anacron# See anacron(8) and anacrontab(5) for details.SHELL=/bin/shPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root# the maximal random delay added to the base delay of the jobsRANDOM_DELAY=45# the jobs will be started during the following hours onlySTART_HOURS_RANGE=3-22# period in days delay in minutes job-identifier command1 5 cron.daily nice run-parts /etc/cron.daily7 25 cron.weekly nice run-parts /etc/cron.weekly@monthly 45 cron.monthly nice run-parts /etc/cron.monthly1 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:
Copy
Ask AI
sudo anacron -n
To focus on anacron-specific log messages, filter the cron log with:
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:
The at command schedules one-time jobs. To schedule a job to run in one minute, enter:
Copy
Ask AI
at 'now + 1 minute'
After this command, you will be prompted to enter the command you wish to run. For example:
Copy
Ask AI
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:
Copy
Ask AI
job 4 at Thu Mar 24 01:22:00 2022
To view the at job log entries:
Copy
Ask AI
sudo grep atd /var/log/cron
You should see log lines indicating the start of the job, for example:
Copy
Ask AI
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:
Copy
Ask AI
at 'now + 1 minute'
Then, when prompted, enter:
Copy
Ask AI
echo "My at job produced this output" | systemd-cat --identifier=at_scheduled_backup
Once the job executes, review the log output with:
Copy
Ask AI
journalctl | grep at_scheduled_backup
Depending on your system settings, you might also find relevant messages in /var/log/messages or /var/log/syslog.
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.