RHCSA Scheduled Tasks & Log Management
π 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! π