In this video, we'll talk about systemd timers.
So systemd provides unit.timer files
that go together with unit.service files
to schedule a service file.
So it's important that the first name of these files
is the same to find the match.
Now, using systemd timers, timers
should be enabled and started and not the service unit.
Because you don't want to run the service unit all the time.
You only want to run it at a specific moment.
Systemd timers are often installed from RPM packages.
And in the timer unit file, the on calendar option
specifies when the service should be started.
Since RL9, systemd timers are the default way
for scheduling recurring services.
Let me show you how it works.
Let's start with systemctl list units minus the timer, which
is showing all the timer units.
And there we can see a couple of timer units,
including log rotate dot timer.
Now, let me use systemctl list unit files log rotate star,
which is showing that there's a log rotate service
and a log rotate timer.
So systemctl status on log rotate dot service
is showing the status of the log rotate service.
And here we can see that it is triggered
by the log rotate timer.
And if you use systemctl status on log rotate dot timer,
we can see that it is active.
And it triggers the log rotate dot service.
So you can nicely find the relation
between the two of them.
Now, let me show you how you can add some.
I'm using dnf install minus y sysstat.
That's a sysstat package, which includes
some nice monitoring utilities.
And I'm going to show you systemctl list unit files sysstat
star.
And there we can see that there is multiple things included.
So what do we have?
We have a sysstat collect dot timer.
And the sysstat collect dot timer, well,
probably matches the sysstat collect dot service.
Is that so?
Well, let's figure it out using systemctl cat
on sysstat collect dot timer.
And what do we see?
We see that this is very simple.
It only has an on calendar.
On calendar indicates that it should start every 10 minutes.
And it is wanted by the sysstat dot service.
So we don't see anything else.
And that means that if you are going to start and enable this,
it will trigger the sysstat collect dot service whenever
the 10 minutes are over.
You know what?
Let's just do that.
Systemctl enable minus minus now sysstat collect dot timer.
Oh, that's a typo.
And there we go.
And that will make sure that several utilities
have useful data to analyze whenever you want to do that.
Now, when you work with systemd timers,
you need to understand how to activate these timers.
The systemd timer on calendar option
uses a rich language to express when the timer should activate.
A rich language that means humanly easy to understand.
For instance, you can use on calendar is star colon 0 0
slash 10 to run it every 10 minutes.
Or on calendar is 2026 dash star dash star,
which means every day in the year 2026.
9 colon 9 comma 19 29 colon 30, meaning at hour 9, minute 9,
19, 29, and second 30, it will run it.
You also have other cool options,
like on unit activate sec.
That will start the unit a specific time
after the unit was last activated.
Or on boot sec and on startup sec,
which will start the unit a specific time after booting.
If you want more information about the time format
to be used, you can read the man page, systemd.time.
That's about the time format, not
to be confused with systemd.timer,
which is a man page that provides
generic information about timers.
Now, let me show you how you can edit systemd timers, which
also applies for other units.
So to verify current settings, you
would always start by using systemctlcatunit.type,
because that will concatenate all the different configuration
files for your unit.
And then you can edit settings using systemctledit
on your unit.
And that will create a drop-in file in etc.systemunit.type.d.
You don't really need to worry about these drop-in files,
as long as you use the systemctl tools, like systemctlcat
and systemctledit, then systemd will take care of it
automatically for you.
Now, next, you can use export editor.
You use binvim to one of your startup files of bash
if you want to use vim instead of nano.
And if you want to ensure that systemd loads the changes,
you can always use systemctldaemonreload.
Well, I need to tell you that systemctldaemonreload
is rarely used nowadays on systemd services.
Systemd has become quite intelligent,
and it will pick up these changes automatically for you.
Now, let me show you how you can create a timer-based solution
completely all by yourself.
So cat, greater than, greater than, etc, systemd,
system, touchfile.service.
I'm creating my own service.
Smaller than, smaller than, EOF.
And then I have my prompt in which
I'm going to define my unit.
Description is demo unit.
Don't need anything else.
And then I have the service.
And type is one shot.
We have seen type is simple before.
Type is simple for simple commands.
One shot is like simple, but it will make sure
that the next unit that is going to be started
waits for this one to complete.
Then xxstart.
To specify what it should be doing,
well, it should be using userbin, touchtmp,
myfile.txt.
And then I'm using EOF to complete the right procedure.
So that creates a service file.
So now I'm going to do something quite similar,
but this time for etc, systemd, system, touchfile.timer.
Smaller than, smaller than, EOF.
So here I create my timer.
Also, the timer, like any unit, has a unit section
where you can put a description.
It's demo timer.
And then I want what?
Well, I want my timer.
And I want it to be on calendar is star colon 00 slash 01.
EOF to end this file.
So now we have created these files.
Let me pop the next slide so that you
can see the next steps.
And let me show you.
So as I manually created all of these files,
it's a smart idea to use systemctl daemon reload.
I would say, in case of doubt, whenever you think,
hey, systemd picking this up automatically,
just use systemctl daemon reload.
Then you know it for sure.
And next, I'm using systemctl start touchfile.timer.
And systemctl status touchfile.service.
As you can see, it is inactive and dead,
but it's triggered by touchfile.timer.
And then I can use watch ls minus l tmp myfile.txt.
The watch command is running every two seconds by default.
And look, it's already there.
You will notice that whenever the minute is over,
the file will be created again.
And the timestamp will change.
So now we have 22 colon 09.
And at minute 22 10, we should see the new timestamp 22 10,
because that is what touch is doing.
Touch is creating an empty file if the file does not yet exist.
If the file already exists, it updates
the timestamp of that file.
And that's what we should see in a few seconds.
There you go, 22 10.
So that's how it works.
Now, we might as well stop it, because this is not really
the most useful service.
So stop touchfile.timer, and now it's gone.