DEV Community

Cover image for Create a Custom systemd Service and Timer on Red Hat Linux
shamain anjum
shamain anjum

Posted on

Create a Custom systemd Service and Timer on Red Hat Linux

*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

  • 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

✔ Shell scripting basics

✔ Service creation and management with systemctl

✔ Timer creation with systemd

✔ Troubleshooting and log analysis with journalctl

1️⃣ 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

Image description

Make it executable:
sudo chmod +x /opt/scripts/log_uptime.sh

2️⃣ 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

3️⃣ 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

Image description

4️⃣ Start and Enable the Timer

sudo systemctl daemon-reload
sudo systemctl enable --now log-uptime.timer

Image description

Check status:
systemctl list-timers

Image description

Verify logs:
cat /var/log/uptime.log

Image description

Check systemd logs:
journalctl -u log-uptime.service

Image description

🧪 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

✅ Recap

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.

Top comments (0)