Understanding Systemd Timers

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
The fstab file in Linux stands for "filesystem table" and is commonly found in the /etc directory. It typically lists all available disk partitions and other types of file systems, their mountpoints and mount options. fstab is meant to be only read b...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

Systemd is a software suite that provides a standard process for controlling what programs run when a Linux system boots up. It starts the core programs running and a journal of system activity, the network stack, a cron-style job scheduler, user logins, and many other jobs.
Systemd timers are a part of this systmed suite and they enable us to schedule tasks to be executed at specified times or intervals, similar to cron jobs but with more features and tight integration with the rest of the systemd ecosystem.
Cron is more of a stand-alone thing whereas systemd timers are part of systemd. Therefore systemd timers can be used only on distributions with systemd systems while cron jobs can be used on distributions either with systemd systems or non-systemd systems.
Systemd timers have additional features such as support for complex calendar-based events, built-in support for catching up on missed jobs, a built-in mechanism for dependency management or condition checks before job execution, and integration with systemd’s journal which provides centralized and unified logging etc.
Additional features of systemd timers come with added complexity. Therefore, systemd timers are a bit more involved meaning more to learn while cron jobs are much simpler to understand and implement.
Which one to use depends on the complexity of the task and which of the two solutions is a better fit for your project on a case-by-case basis.
To create a systemd timer two files are required: a .timer file to schedule the task and a .service file for the task.
Timer units are the actual timer definitions. They define when and how often a task should be executed. The timer units have a .timer extension and are typically stored in the /etc/systemd/system directory. An example of weather-report.timer unit file is given below.

Service units are the tasks that are executed when the timer is triggered. Service units have a .service extension and are typically stored in the /etc/systemd/system directory. An example of weather-report.service unit file is provided below.

When you have a .service file with the same name as the .timer file in the directory, with the only difference being the file extension, systemd automatically considers them to be related. Usually, for each timer, there is a corresponding service file that gets executed when the timer triggers.
Real-time timers are based on the actual real-world clock time. They can be set to trigger at specific times, such as "every day at 2:00 AM".
Monotonic timers are based on a timer relative to a certain point, such as "15 minutes after boot".
Understanding systemd timers involve knowing how to create and manage them. This hands-on exercise shows how to create a systemd timer that will run the wall command automatically on a specific schedule.
Create a new weather-report.service unit file:
sudo vim /etc/systemd/system/weather-report.service
Copy and paste the following code into the file and save it:
[Unit]
Description=Weather Report Service
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/bin/sh -c 'sudo wall $(curl wttr.in?format=3)'
[Install]
WantedBy=default.target
This simple file executes the wall command, which stands for "write to all". The wall command sends a message to all users currently logged into the system and the message is supposed to be the output of curl wttr.in?format=3, which queries the wttr.in service for weather information in a brief format. This can be particularly useful for system administrators who need to notify users about system maintenance, reboots, or other important events.
Refresh systemd so that it’s aware of the fact we added a new service file:
sudo systemctl daemon-reload
Now, when we want to send a weather report to every user on the system, all we need to do is start the systemd service for it:
sudo systemctl start weather-report.service

Create a new weather-report.timer unit file the type of which will be a timer:
sudo vim /etc/systemd/system/weather-report.timer
Copy and paste the following code into the file and save it:
[Unit]
Description=Weather Report Service
[Timer]
OnCalendar=Mon..Fri 08:32
Persistent=true
[Install]
WantedBy=timers.target
Under the timer section, we have OnCalendar here. We’re giving it a range starting with Monday and ending with Friday. Also, we’re setting it for 08:32 am, which means the timer will trigger weather-report.service file every weekday at 08:32 am.
If the server is down for any reason the timer won’t trigger. Enabling the Persistent=true option, allows the task to run once the server starts up next in case the OnCalendar time is missed.
.service extension instead of a .timer extension. Therefore we don’t tell the timer which service file to start, it already knows what it is looking for. All we have to do is match the naming scheme.In the install section, targets are another type of unit file, and when you link them like this, the current file will inherit specific characteristics from the target.
Refresh systemd once more so that it knows we added a new timer unit file:
sudo systemctl daemon-reload
Finally, start and enable the timer with one single command:
sudo systemctl enable --now weather-report.timer
Check the status of your timer:
systemctl status weather-report.timer
