7. Deploy, Configure, and Maintain Systems
πRed Hat Certified System Administrator (RHCSA β EX200)
In Linux, automating tasks is essential for system administration. You rarely want to do everything manually, so Linux provides tools to schedule jobs to run automatically. The main tools for this are:
- cron β for recurring jobs
- at β for one-time jobs
- systemd timer units β newer method, works with systemd, both recurring and one-time jobs
1. Using cron
cron is a service that runs in the background (daemon) and executes commands at scheduled intervals.
How it works
cronreads crontab files which list commands to run and when to run them.- Each user can have their own crontab, including root.
Crontab File Format
Each line in a crontab file follows this pattern:
* * * * * command
- - - - -
| | | | |
| | | | +---- Day of week (0-7) (Sunday=0 or 7)
| | | +------ Month (1-12)
| | +-------- Day of month (1-31)
| +---------- Hour (0-23)
+------------ Minute (0-59)
Example: Run a backup script every day at 2:30 AM:
30 2 * * * /usr/local/bin/backup.sh
Commands to Manage crontab
crontab -eβ edit your own crontabcrontab -lβ list your crontabcrontab -rβ remove your crontab
Special Strings in crontab: These make scheduling easier:
@rebootβ run once at boot@dailyβ run once per day@weeklyβ run once per week@monthlyβ run once per month@hourlyβ run once per hour
Example:
@daily /usr/local/bin/cleanup_logs.sh
System-wide crontab
- Located in
/etc/crontab - Also, scripts can be placed in
/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthlyfor automatic execution.
2. Using at
at is for scheduling one-time jobs, unlike cron which is recurring.
How to use at
- Install
atif not installed:
sudo yum install at
- Start the service (if not running):
sudo systemctl enable --now atd
- Schedule a command:
echo "/usr/local/bin/script.sh" | at 14:30
or interactively:
at 14:30
at> /usr/local/bin/script.sh
at> <Ctrl+D>
- List pending jobs:
atq
- Remove a scheduled job:
atrm <job_number>
Time formats supported:
HH:MM(24-hour format)now + 10 minutestomorrownext week
Example: Run a patch script at 3 PM today:
at 15:00
at> /usr/local/bin/apply_patches.sh
at> <Ctrl+D>
3. Using systemd Timer Units
Modern Linux uses systemd, which can replace cron and at for both recurring and one-time jobs. This is becoming preferred in Red Hat systems.
Components
- Service unit (
.service) β defines what to run - Timer unit (
.timer) β defines when to run it
Example Setup
- Create a service file:
/etc/systemd/system/backup.service
[Unit]
Description=Daily Backup Service[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
- Create a timer file:
/etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily at 2:30 AM[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true[Install]
WantedBy=timers.target
- Enable and start the timer:
sudo systemctl enable --now backup.timer
- Check timer status:
systemctl list-timers
OnCalendar values:
*-*-* 02:30:00β daily at 2:30 AMMon *-*-* 12:00:00β every Monday at noon
Advantages over cron/at:
- Integrated with systemd logs (
journalctl) - Can trigger based on events, not just time
- Better error handling
4. Practical IT Examples
- Cron: Schedule log rotation every night at 1 AM:
0 1 * * * /usr/sbin/logrotate /etc/logrotate.conf
- At: Schedule a server restart at 11 PM for maintenance:
echo "shutdown -r now" | at 23:00
- Systemd Timer: Automatically run a backup script every day at 2 AM and log output:
/etc/systemd/system/backup.service
/etc/systemd/system/backup.timer
5. Key Exam Points
- Understand differences:
cronβ recurring tasksatβ one-time taskssystemd timerβ modern replacement, more flexible
- Know commands:
crontab -e/-l/-r,at,atq,atrm,systemctl enable/start/status timer.service
- Crontab syntax β must know the 5 fields and special strings.
- Systemd timer units β create
.service+.timer, enable/start, check timers. - Always ensure services are running:
systemctl status crond
systemctl status atd
systemctl status backup.timer
β This covers everything for RHCSA 7.1 β scheduling tasks. If you master these commands, understand the syntax, and can create cron/at/systemd timers, youβll pass this part of the exam.
