> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Verify completion of scheduled jobs

> 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.

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

## Verifying Cron Jobs

Begin by reviewing your user-specific crontab. To open your crontab for editing, run:

```bash theme={null}
crontab -e
```

In this example, two commands have been added to the crontab, scheduled to run every minute:

```bash theme={null}
* * * * * /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:

```bash theme={null}
sudo cat /var/log/cron
```

You should see log entries resembling the following:

```bash theme={null}
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:

```bash theme={null}
sudo grep CMD /var/log/cron
```

When you are finished testing or need to remove the current crontab settings, clear them with:

```bash theme={null}
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:

```bash theme={null}
cat /etc/crontab
```

You will see content similar to this:

```bash theme={null}
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:

```bash theme={null}
sudo vi /etc/anacrontab
```

You should see content similar to:

```bash theme={null}
# /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:

```bash theme={null}
sudo anacron -n
```

To focus on anacron-specific log messages, filter the cron log with:

```bash theme={null}
sudo grep anacron /var/log/cron
```

For example, you'll see log entries like:

```bash theme={null}
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:

```bash theme={null}
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`:

```bash theme={null}
sudo vi /etc/anacrontab
```

Update the `test_job` line as follows:

```bash theme={null}
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:

```bash theme={null}
sudo anacron -n -f
```

Then, view the system log to find the output:

```bash theme={null}
journalctl -e
```

You should see entries similar to:

```text theme={null}
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:

```bash theme={null}
at 'now + 1 minute'
```

After this command, you will be prompted to enter the command you wish to run. For example:

```bash theme={null}
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:

```text theme={null}
job 4 at Thu Mar 24 01:22:00 2022
```

To view the at job log entries:

```bash theme={null}
sudo grep atd /var/log/cron
```

You should see log lines indicating the start of the job, for example:

```text theme={null}
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`:

```bash theme={null}
at 'now + 1 minute'
```

Then, when prompted, enter:

```bash theme={null}
echo "My at job produced this output" | systemd-cat --identifier=at_scheduled_backup
```

Once the job executes, review the log output with:

```bash theme={null}
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](https://www.centos.org/docs/).

Thank you for reading, and please proceed to the next section for additional demonstrations and related topics.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/736506db-a70d-463d-a061-74c768d309b0/lesson/932ac452-6ea8-4fe9-8416-6e1a3903265f" />

  <Card title="Practice Lab" icon="installation" cta="Learn more" href="https://learn.kodekloud.com/user/courses/red-hat-certified-system-administrator-rhcsa/module/736506db-a70d-463d-a061-74c768d309b0/lesson/f2c537b4-1787-4f9e-9f03-578aa7f0b98e" />
</CardGroup>
