Linux Task Scheduling Using systemd Timers on RHEL

Linux Task Scheduling Using systemd Timers on RHEL


Table of Contents

  1. Introduction
  2. Prerequisites
  3. Implementation
  4. 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

ConfigurationValue
Operating SystemAlmaLinux 9.8
Source Directory/var/www/html
Backup Directory/backup
Backup ScheduleDaily at 1:00 AM
Service Namebackup.service
Timer Namebackup.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.

  1. 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
  2. Create a systemd service
    • 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=oneshot Executes the task once and exits.
      ExecStart Specifies the script or command to execute.
  3. Create a systemd timer
    • 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
      OnCalendar Schedules the backup every day at 1:00 AM.
      Persistent=true Runs a missed scheduled task after the system becomes active again.
      Unit Specifies the service triggered by the timer.
      WantedBy Allows the timer to start automatically during system boot.
  4. Reload systemd configuration

    After creating the service and timer files, reload systemd so it recognizes the new configuration.

    systemctl daemon-reload
  5. Enable and start the timer

    Enable the timer to start automatically during system boot and start it immediately.

    systemctl enable --now backup.timer
  6. Verify the timer
    • List scheduled systemd timers
      systemctl list-timers
    • Check the timer status
      systemctl status backup.timer
  7. Check backup service logs

    Use journalctl to check the execution logs of the backup service.

    journalctl -u backup.service
  8. 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.service

    Check 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.