Configuring a Monotonic Timer Unit

Monotonic timers that activate after a time span relative to a varying starting point, such as a boot event, or when a particular systemd unit becomes active. These timer units stop if the computer is temporarily suspended or shut down. Monotonic timers are configured by using the OnTypeSec option, where Type is the name of the event to which the timer is related. Common monotonic timers include OnBootSec and OnUnitActiveSec.

  • If needed, create a .service file that defines the service to be triggered by the timer unit. In the following procedure, the sample service is /etc/systemd/system/update.service which is a service unit that runs an update script.

    For more information about creating service units, see Creating a User-Based systemd Service.

  • Decide the time and frequency for running the service. In this procedure, the timer is configured to run the service 10 minutes after a system boot, and every 2 hours from when the service is last activated.

This task shows you how to create a system timer to trigger a service to run at specific events, which are when the system boots or after 2 hours have lapsed from the timer's activation.

  1. Create the /etc/systemd/system/update.timer with the following content:
    [Unit]
    Description="Run the update.service every two hours from Mon to Fri."
    
    [Timer]
    OnBootSec=10min
    OnUnitActiveSec=2h
    Unit=update.service
    
    [Install]
    WantedBy=multi-user.target

    For more timer options that you can configure in the system timer, see the systemd.timer(5) and systemd.time(7) manual pages.

  2. Check that all the files related to this timer are configured correctly.
    systemd-analyze verify /etc/systemd/system/update.*

    Any detected errors are reported on the screen.

  3. Start the timer.
    sudo systemctl start update.timer

    This command starts the timer for the current session only.

  4. Ensure that the timer starts when the system is booted.
    sudo systemctl enable update.timer