- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
*Day 28: Create a Custom systemd Service and Timer on Red Hat Linux *
Today’s project focused on a powerful automation tool for Red Hat Linux admins: creating a custom systemd service + timer.
This lets you automate any script or task at system startup or on a schedule, without relying on traditional cron jobs.
This task is also a great match for RHCSA exam scenarios!
? Objective
? RHCSA Skills Covered
Shell scripting basics
Service creation and management with systemctl
Timer creation with systemd
Troubleshooting and log analysis with journalctl
Create a Custom Script
Create a script to log system uptime every 5 minutes.
sudo mkdir -p /opt/scripts
sudo nano /opt/scripts/log_uptime.sh
Example script:
!/bin/bash
echo "$(date): System uptime is: $(uptime)" >> /var/log/uptime.log
Make it executable:
sudo chmod +x /opt/scripts/log_uptime.sh
Create a Custom systemd Service
sudo nano /etc/systemd/system/log-uptime.service
Example unit file:
[Unit]
Description=Log system uptime
[Service]
Type=oneshot
ExecStart=/opt/scripts/log_uptime.sh
Create a systemd Timer
sudo nano /etc/systemd/system/log-uptime.timer
Example timer file:
[Unit]
Description=Run uptime logger every 5 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=log-uptime.service
[Install]
WantedBy=timers.target
Start and Enable the Timer
sudo systemctl daemon-reload
sudo systemctl enable --now log-uptime.timer
Check status:
systemctl list-timers
Verify logs:
cat /var/log/uptime.log
Check systemd logs:
journalctl -u log-uptime.service
? Try It Yourself
Recap
? Why This Matters (RHCSA)
systemd skills are now explicitly tested on the RHCSA exam.
This project touches:
You’ll likely face a systemd troubleshooting or service creation task in the real exam.
Today’s project focused on a powerful automation tool for Red Hat Linux admins: creating a custom systemd service + timer.
This lets you automate any script or task at system startup or on a schedule, without relying on traditional cron jobs.
This task is also a great match for RHCSA exam scenarios!
? Objective
- Write a simple shell script
- Create a custom systemd service to run the script
- Configure a systemd timer to run the service automatically
- Verify the timer execution and check logs
? RHCSA Skills Covered
Create a script to log system uptime every 5 minutes.
sudo mkdir -p /opt/scripts
sudo nano /opt/scripts/log_uptime.sh
Example script:
!/bin/bash
echo "$(date): System uptime is: $(uptime)" >> /var/log/uptime.log
Make it executable:
sudo chmod +x /opt/scripts/log_uptime.sh
sudo nano /etc/systemd/system/log-uptime.service
Example unit file:
[Unit]
Description=Log system uptime
[Service]
Type=oneshot
ExecStart=/opt/scripts/log_uptime.sh
sudo nano /etc/systemd/system/log-uptime.timer
Example timer file:
[Unit]
Description=Run uptime logger every 5 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=log-uptime.service
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now log-uptime.timer
Check status:
systemctl list-timers
Verify logs:
cat /var/log/uptime.log
Check systemd logs:
journalctl -u log-uptime.service
? Try It Yourself
- Change OnUnitActiveSec=5min to 1min and test faster execution
- Replace the script with any custom automation (e.g., backup, cleanup)
- Use systemctl status log-uptime.timer to verify next scheduled run
| Task | Tool/Command |
|---|---|
| Create shell script | nano /opt/scripts/log_uptime.sh |
| Create systemd service | /etc/systemd/system/log-uptime.service |
| Create systemd timer | /etc/systemd/system/log-uptime.timer |
| Enable and start timer | systemctl enable --now log-uptime.timer |
| Check logs | cat /var/log/uptime.log, journalctl |
? Why This Matters (RHCSA)
systemd skills are now explicitly tested on the RHCSA exam.
This project touches:
- Unit creation
- Timer configuration
- Troubleshooting failed services
You’ll likely face a systemd troubleshooting or service creation task in the real exam.