Linux Task Scheduling Using systemd Timers on RHEL
Table of Contents
- Introduction
- Prerequisites
- Implementation
- Conclusion
1. Introduction
In this tutorial, we will configure a systemd timer on RHEL-based Linux distributions to automate daily tasks.
We will use a systemd service to execute a backup script and a systemd timer to schedule the backup automatically every day at 1:00 AM.
Unlike traditional Cron jobs, systemd timers integrate directly with the systemd service manager, providing centralized service management and logging through journalctl.
2. Prerequisites
- A RHEL-based Linux server (RHEL / AlmaLinux / Rocky Linux).
- Root or sudo privileges.
- Basic Linux command-line knowledge.
- systemd installed and running.
- A source directory containing data to back up.
Example Environment
| Configuration | Value |
|---|---|
| Operating System | AlmaLinux 9.8 |
| Source Directory | /var/www/html |
| Backup Directory | /backup |
| Backup Schedule | Daily at 1:00 AM |
| Service Name | backup.service |
| Timer Name | backup.timer |
3. Implementation
Scenario:
An administrator wants to automatically back up important application data every day at 1:00 AM.
Instead of manually running the backup script, we will configure a systemd timer to execute it automatically.
-
Create a backup script
-
Create the backup script
vi /usr/local/bin/backup.sh -
Add the following configuration
#!/bin/bash BACKUP_DIR="/backup" SOURCE_DIR="/var/www/html" DATE=$(date +%F) mkdir -p "$BACKUP_DIR" tar -czf "$BACKUP_DIR/website-$DATE.tar.gz" "$SOURCE_DIR"
-
Make the script executable
chmod +x /usr/local/bin/backup.sh
-
Create the backup script
-
Create a
systemdservice-
Create the service file
vi /etc/systemd/system/backup.service -
Add the following configuration
[Unit] Description=Daily Website Backup [Service] Type=oneshot ExecStart=/usr/local/bin/backup.sh
Configuration Description Type=oneshotExecutes the task once and exits. ExecStartSpecifies the script or command to execute.
-
Create the service file
-
Create a
systemdtimer-
Create the timer file:
vi /etc/systemd/system/backup.timer -
Add the following configuration:
[Unit] Description=Daily Website Backup Timer [Timer] OnCalendar=*-*-* 01:00:00 Persistent=true Unit=backup.service [Install] WantedBy=timers.target
Configuration Description OnCalendarSchedules the backup every day at 1:00 AM. Persistent=trueRuns a missed scheduled task after the system becomes active again. UnitSpecifies the service triggered by the timer. WantedByAllows the timer to start automatically during system boot.
-
Create the timer file:
-
Reload
systemdconfigurationAfter creating the service and timer files, reload
systemdso it recognizes the new configuration.systemctl daemon-reload
-
Enable and start the timer
Enable the timer to start automatically during system boot and start it immediately.
systemctl enable --now backup.timer
-
Verify the timer
-
List scheduled
systemdtimerssystemctl list-timers
-
Check the timer status
systemctl status backup.timer
-
List scheduled
-
Check backup service logs
Use journalctl to check the execution logs of the backup service.
journalctl -u backup.service
-
Test the service manually
You do not need to wait until 1:00 AM to test the backup.
Manually start the service:
systemctl start backup.serviceCheck the backup directory to verify that the backup file was created:
ls -lh /backup/
Result: You should now have a backup archive inside
/backup/.
4. Conclusion
We have successfully configured a systemd timer to automate daily website backups on a RHEL-based Linux server.
By using a systemd service and timer, administrators can automate routine tasks such as backups, log cleanup, database maintenance, and monitoring scripts.
Systemd timers also provide better integration with Linux service management and centralized logging through journalctl.