Configuring a Realtime Timer Unit

Realtime timers activate on a calendar event, similar to events in a crontab. The option OnCalendar specifies when the timer runs a service.

  • 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 every 2 hours from Monday to Friday.

This task shows you how to create a system timer to trigger a service to run based on a calendar event. The definition of the calendar event is similar to entries that you put in a cron job.

  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]
    OnCalendar=Mon..Fri 00/2 
    Unit=update.service
    
    [Install]
    WantedBy=multi-user.target

    Defining OnCalendar can vary from a simple wetting such as OnCalendar=weekly definitions that are more detailed. However, the format for defining settings is constant, as follows:

    DayofWeek Year-Month-Day Hour:Minute:Second

    The following definition means "the first 4 days of each month at 12:00 o'clock noon, but only if that day is either a Monday or a Tuesday":

    OnCalendar=Mon,Tue *-*-01..04 12:00:00

    For other ways to define OnCalendar and for more timer options that you can configure in the system timer file, 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