Systemd timers are a feature of the systemd init system on Linux. They are used to schedule and automate the execution of tasks or services at specific times or intervals. Timers are often used for automating periodic maintenance tasks, backups, data synchronization, or any other operation that needs to be run on a schedule.

Here are some key points about systemd timers:

  1. Automation: Systemd timers allow you to automate tasks and services by defining when and how often they should run.
  2. Accuracy: Timers are accurate and can be scheduled with precision down to the second.
  3. Persistent Timers: Timers can be defined as persistent, meaning that if the system is down when the timer is supposed to trigger, it will run the task on the next available opportunity.
  4. Timer Units: A systemd timer is defined in a separate unit file with a .timer extension, and it specifies the schedule and how it activates a corresponding service unit.
  5. Service Units: A timer typically triggers the execution of a service unit, which contains the actual task or command you want to run.
  6. Calendar-Based Scheduling: Timers can be scheduled based on calendar events like specific dates, days of the week, and times of day. You can use OnCalendar to define these schedules.
  7. Relative Time: Timers can also be scheduled in relative time, e.g., after system boot (OnBootSec) or after the previous run of the service (OnUnitActiveSec).
  8. User and System Timers: Timers can be defined for the entire system or for specific users, allowing users to schedule their own tasks without requiring root privileges.
  9. Listing Timers: You can list all active timers and check when they are next scheduled to run using the systemctl list-timers command.
  10. Logs: You can inspect the logs of both timers and the services they activate using journalctl.

Systemd timers offer a powerful way to schedule and automate tasks, and they are widely used for system maintenance and other periodic operations on Linux systems.

Below, I’ll provide a step-by-step guide on how to create a systemd timer:

Step 1: Create a Service Unit and Script

First, you need to have a service unit that you want to run on a schedule. Create a systemd service unit (e.g., my-service.service) that defines the task you want to execute. The service unit file typically goes in the /etc/systemd/system/ directory or /usr/lib/systemd/system/ for system-wide services, or in the ~/.config/systemd/user/ directory for per-user services. Here’s an example of a simple service unit:

[Unit]
Description=Update ipset from Dynamic DNS

[Service]
Type=simple
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=default.target

Make sure to replace /path/to/your/script.sh with the actual path to your script or command.

Here’s an example of a sample script to update the ip address in ipset from dynamic dns:

#!/bin/bash
# Find IP address using dig and store it in $ip

ip=$(dig +short demo.dyn.org)

# Clear the ipset list
sudo ipset flush demolist

# Add the new ip address to the ipset list
sudo ipset add demolist $ip

# Validation you can delete this section if you don't need it.
# echo $ip
output=/tmp/demo.dyn.org.txt
echo $ip >> $output

Make sure to replace those in bold and italic with your own.

Step 2: Create a Timer Unit

Create a systemd timer unit:

[Unit]
Description=Schedule a message every 1 minute
RefuseManualStart=no  # Allow manual starts
RefuseManualStop=no   # Allow manual stops

[Timer]
#Execute job if it missed a run due to machine being off
Persistent=true
#Run 120 seconds after boot for the first time
OnBootSec=120
#Run every 1 minute thereafter
OnUnitActiveSec=60
#File describing job to execute
Unit=updateipsetdyndns.service

[Install]
WantedBy=timers.target

In this example, the Persistent=true directive ensures that if the system is down at the scheduled time, the task will be executed on the next available opportunity.

Step 3: Enable and Start the Timer

Enable the timer to run the service on the specified schedule:

sudo systemctl enable my-timer.timer

Start the timer:

sudo systemctl start my-timer.timer

This will schedule the service to run based on the timer’s configuration.

Step 4: Monitor the Timer

To check the status and next execution time of the timer, you can use the list-timers command:

systemctl list-timers

This command will display a list of active timers and their upcoming run times.

Step 5: Test and Debug

You can inspect the logs of your service to debug any issues:

journalctl -u my-service.service

Remember to adapt these instructions to your specific use case and schedule requirements. Systemd timers provide a flexible and powerful way to automate tasks on a schedule.

Additional userful commands

$ systemctl --user status schedule-test
$ systemctl --user list-unit-files
$ systemctl --user stop schedule-test.service
$ systemctl --user stop schedule-test.timer
$ systemctl --user disable schedule-test.timer
$ systemctl --user stop schedule-test.service
$ systemctl --user disable schedule-test.service
$ systemctl --user daemon-reload
$ systemctl --user reset-failed