Jump to contentJump to page navigation: previous page [access key p]/next page [access key n]

Working with systemd timers

Publication Date: 09/24/2021

1 Environment

  • File Name: task-create-systemd-timers.xml
  • ID: environment-systemd-timers

This document applies to the following products and product versions:

  • SUSE Linux Enterprise Server 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3

  • SUSE Linux Enterprise Server for SAP Applications 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3

  • SUSE Linux Enterprise High Availability Extension 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3

  • SUSE Linux Enterprise High Performance Computing 15 SP3, 15 SP2, 15 SP1, 15 GA

  • SUSE Linux Enterprise Desktop 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3

  • SUSE Linux Enterprise Real Time 15 SP3, 15 SP2, 15 SP1, 15 GA, 12 SP5, 12 SP4, 12 SP3

2 Introduction

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found

From running a backup script at regular intervals to starting a specific process as soon as the machine boots, there are plenty of tasks that require scheduling on a Linux system. cron has long been the go-to tool for all scheduling needs, but the switch to systemd introduced another way to schedule tasks: systemd timer units.

Similar to cron, systemd timer units provide a mechanism for scheduling jobs on Linux. Following the instruction below, you can learn how to create a systemd timer.

3 Requirements

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found
  • Basic understanding of systemd.

  • Root privileges.

4 Creating a systemd timer

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found

The following procedure describes how to set up a timer that triggers the helloworld.sh shell script.

Procedure 1: Creating a systemd timer
  1. Before you set up a systemd timer unit, you need to create a systemd service unit that controls the shell script. To do this, open a new text file for editing and add the following service unit definition:

    [Unit]
    Description="Hello World script"
    
    [Service]
    ExecStart=/usr/local/bin/helloworld.sh
  2. Save the file under the name helloworld.service in the directory /etc/systemd/system/.

  3. Open a new text file for editing and add the following timer definition:

    [Unit]
    Description="Run Hello World script"
    
    [Timer]
    OnBootSec=5min
    OnUnitActiveSec=24h
    Unit=helloworld.service
    
    [Install]
    WantedBy=multi-user.target

    The [Timer] section in the example above specifies what service to trigger (helloworld.service) and when to trigger it. In this case, the option OnBootSec specifies a monotonic timer that triggers the service five minutes after the system boot, while the option OnUnitActiveSec triggers the service 24 hours after the service has been activated (that is, the timer will trigger the service once a day). Finally the option WantedBy specifies that the timer should start when the system has reached the multi-user target.

  4. Save the timer unit file under the name helloworld.timer in the /etc/systemd/system/ directory. To check the correctness of the created unit files, run the following command:

    tux > sudo  systemd-analyze verify /etc/systemd/system/helloworld.*

    If the command returns no output, the files have passed the verification successfully.

  5. To start the timer, use the command sudo systemctl start helloworld.timer. To enable the timer on boot, run the command sudo systemctl enable helloworld.timer.

5 Troubleshooting

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found

The best way to make sure that you catch all issues beforehand, is to verify systemd timer units before enabling them. Running the systemd-analyze verify command can help you to identify and fix potential problems.

6 Next steps

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found
  • Learn more about systemd

7 Related topics

  • File Name: task-create-systemd-timers.xml
  • ID: no ID found
  • systemd conceptual overview

  • Creating and managing systemd service units

Print this page