Linux System Administration for Beginners
Operation of Running Systems
Verify completion of scheduled jobs
Ensuring your scheduled tasks run as expected is critical for system reliability. CentOS Stream 8 provides three primary schedulers—cron
, anacron
, and at
—all of which log their execution events by default. In this guide, you’ll learn how to confirm job completion, capture outputs, and troubleshoot using system logs and the journal.
Note
You may need sudo
privileges to view system logs (/var/log/cron
, /var/log/messages
) or to edit system crontabs (/etc/crontab
, /etc/anacrontab
).
Table of Contents
- Verifying cron Jobs
- Inspecting the System-wide crontab
- Verifying anacron Jobs
- Capturing anacron Job Output
- Verifying at-scheduled Jobs
- Log Files Reference
- References
1. Verifying cron Jobs
- Open your user crontab for editing:
crontab -e
- Add two simple
echo
commands to run every minute:* * * * * /bin/echo "Just testing cron" * * * * * /bin/echo "Just testing cron again"
- Save and exit. Wait at least one minute, then review the cron log:
sudo cat /var/log/cron
- You should see entries similar to:
Mar 24 01:05:01 LFCS-CentOS CROND[9203]: (aaron) CMD (/bin/echo "Just testing cron again") Mar 24 01:05:01 LFCS-CentOS CROND[9204]: (aaron) CMD (/bin/echo "Just testing cron")
- To filter only executed commands, use
grep
:sudo grep CMD /var/log/cron
2. Inspecting the System-wide crontab
View the system-wide crontab to understand global settings and syntax:
cat /etc/crontab
Example excerpt:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# ┌───────── minute (0 - 59)
# │ ┌─────── hour (0 - 23)
# │ │ ┌───── day of month (1 - 31)
# │ │ │ ┌─── month (1 - 12)
# │ │ │ │ ┌─ day of week (0 - 6)
# * * * * * user-name command to be executed
MAILTO
controls where cron sends job output via email.- Global environment variables apply to all entries.
3. Verifying anacron Jobs
Anacron ensures periodic jobs run even if the system was powered off when they were scheduled.
- Edit the anacrontab:
sudo vi /etc/anacrontab
- Example
/etc/anacrontab
with a customtest_job
:SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root RANDOM_DELAY=45 START_HOURS_RANGE=3-22 #period delay 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"
- Force anacron to execute all jobs immediately:
sudo anacron -n
- Review anacron entries in the cron log:
Example output:sudo grep anacron /var/log/cron
Mar 24 01:01:01 LFCS-CentOS anacron[8903]: Anacron started on 2022-03-24 Mar 24 01:01:01 LFCS-CentOS anacron[8903]: Will run job `test_job` in 12 min. Mar 24 01:13:01 LFCS-CentOS anacron[8903]: Job `test_job` started Mar 24 01:13:01 LFCS-CentOS anacron[8903]: Job `test_job` terminated (produced output)
- To view only your custom job entries:
sudo grep test_job /var/log/cron
4. Capturing anacron Job Output
By default, anacron logs only “produced output.” To capture the full output in the system journal:
- Modify the
test_job
command to usesystemd-cat
:1 10 test_job /bin/echo "Testing anacron" | systemd-cat --identifier=test_job
- Save changes and rerun jobs:
sudo anacron -n -f
- Query the journal for your job identifier:
You’ll see both the start/stop messages and the actual command output:journalctl -e | grep test_job
Mar 24 01:13:01 LFCS-CentOS test_job[8903]: Testing anacron
Warning
Your system must be running systemd
with journalctl
available. If not, output will not be captured in the journal.
5. Verifying at-scheduled Jobs
The at
daemon handles one-off scheduled tasks.
- Schedule a job to run in one minute:
at 'now + 1 minute' warning: commands will be executed using /bin/sh at> echo "My at job produced this output" | systemd-cat --identifier=at_scheduled_backup at> <EOT> job 5 at Thu Mar 24 01:22:00 2022
- Confirm the
atd
daemon started your job:
Example:sudo grep atd /var/log/cron
Mar 24 01:22:00 LFCS-CentOS atd[10028]: Starting job 5 (a0000501la3224z) for user 'aaron' (1000)
- View the job output in the journal:
journalctl | grep at_scheduled_backup
Mar 24 01:22:00 LFCS-CentOS at_scheduled_backup[10028]: My at job produced this output
- On systems without
journald
, check/var/log/messages
or/var/log/syslog
forat
entries.
Log Files Reference
Log File | Purpose | Example Command |
---|---|---|
/var/log/cron | cron & anacron scheduling events | sudo grep CMD /var/log/cron |
/var/log/messages | System-wide messages (including at) | sudo grep atd /var/log/messages |
Journal (journald ) | Captured via systemd-cat identifiers | `journalctl |
References
Watch Video
Watch video content
Practice Lab
Practice lab