RHCSA Scheduled Tasks & Log Management

Expertise in Cloud, Networking & DevOps
Photo by Luis Lopes / Unsplash

πŸ“Œ Introduction

Managing scheduled tasks and logs effectively is crucial for maintaining system performance and security. The RHCSA exam includes topics such as configuring cron jobs, systemd timers, and log monitoring. This guide provides a detailed walkthrough of these essential Linux administration skills.


πŸ“– What You’ll Learn in This Guide

πŸ”Ή Configuring and scheduling cron jobs for automated tasks

πŸ”Ή Setting up systemd timers as an alternative to cron jobs

πŸ”Ή Managing and monitoring system logs using journalctl and rsyslog

πŸ”Ή Troubleshooting and optimizing scheduled tasks and logs


πŸ•° Managing Scheduled Tasks with Cron Jobs

1️⃣ Understanding Cron Jobs

Cron jobs allow users and administrators to schedule commands or scripts at specified times. The cron daemon (crond) runs in the background and executes tasks based on pre-defined schedules.

To check if crond is running:

sudo systemctl status crond

If it’s inactive, enable and start it:

sudo systemctl enable --now crond

2️⃣ Creating and Managing Cron Jobs

To list scheduled cron jobs for the current user:

crontab -l

To edit the cron jobs:

crontab -e

Cron syntax follows this format:

* * * * * command-to-execute
| | | | |
| | | | └── Day of the week (0-6, Sun=0)
| | | └──── Month (1-12)
| | └────── Day of the month (1-31)
| └──────── Hour (0-23)
└────────── Minute (0-59)

3️⃣ Example Cron Jobs

  • Run a script every day at midnight:
0 0 * * * /home/user/backup.sh
  • Run a command every Monday at 3 AM:
0 3 * * 1 /usr/bin/cleanup.sh
  • Run a task every 10 minutes:
*/10 * * * * /home/user/script.sh

πŸ” Best Practice: Redirect output and errors to a log file for troubleshooting:

0 2 * * * /home/user/script.sh > /var/log/script.log 2>&1

⏲ Scheduling Tasks with Systemd Timers

Unlike cron, systemd provides timers for scheduling tasks with greater flexibility and logging capabilities.

1️⃣ Creating a Systemd Timer

Create a service file in /etc/systemd/system/example-task.service:

[Unit]
Description=Example Scheduled Task

[Service]
ExecStart=/home/user/task.sh

Then, create a corresponding timer file in /etc/systemd/system/example-task.timer:

[Unit]
Description=Runs example task every day at 2 AM

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target

Enable and start the timer:

sudo systemctl enable --now example-task.timer

To check active timers:

systemctl list-timers --all

πŸ” Best Practice: Use systemd timers instead of cron when logging and execution tracking are required.


πŸ“œ Log Management & Monitoring

1️⃣ Viewing Logs with journalctl

System logs are managed by systemd-journald. To view logs:

journalctl -xe  # Show recent system logs with errors

To view logs for a specific service:

journalctl -u sshd --since today

2️⃣ Monitoring Logs in Real-Time

To follow log updates in real-time:

journalctl -f

For syslog-based systems using rsyslog, logs are stored in /var/log/.

To view authentication logs:

tail -f /var/log/auth.log

3️⃣ Managing Log Size & Retention

To check journal log size:

journalctl --disk-usage

To clear old logs and free up space:

journalctl --vacuum-time=7d  # Remove logs older than 7 days

πŸ” Best Practice: Automate log rotation using logrotate for efficient log retention.


πŸ›  Essential Practice for RHCSA

βœ… Schedule automated tasks using cron jobs and systemd timers

βœ… Configure, enable, and monitor scheduled jobs

βœ… Analyze system logs using journalctl and rsyslog

βœ… Optimize log storage and implement log rotation

βœ… Troubleshoot system logs to diagnose issues


πŸ“Œ Next Article: RHCSA Storage Management

πŸ“© Subscribe to our blog for more RHCSA tutorials and updates! πŸš€

Read more