Let's talk about sysmd timers. That's the default that you will find on Red Hat family Linux.
So I'm going to use systemctl t timer list units which will
show all timer units that are currently active. And here we can
see a couple of these timer units including for instance logrotate timer.
So if I'm going to check what is going on, next
thing that you need to know is systemctl list units logrotate.
How does it work? Well, we have logrotate timer and logrotate timer is going to trigger
something else. But hey, what is something else? Well, I don't see it as a unit.
Let's have a look at systemctl list unit unit fails
and there we can see that suddenly logrotate service appears.
So what is the relation?
The timer triggers the service. They need to have the same first name. You
do not activate the service, you activate the timer and that's what you are
going to see. If I would use systemctl status on logrotate service then you
can see it has active inactive, but it has triggered by.
Hey, this guy will be triggered by logrotate timer.
And then if we check what the timer is doing itself. Well, systemctl cat on logrotate timer
is showing that the timer is activating daily and it's persistent. So it will remember what it
has done and randomized delay. Second it will run more or less within the same hour.
And that is what is making sure that logrotate is running on a daily basis.
Now let's have a look at systemctl list
unit files this time minus the timer there
we can see that different timers are available.
Is there something that I want to activate?
Well, I was thinking fstream timer but fstream timer is already activated.
That is so disappointing.
So I need something that makes sense in a different way.
But be careful, you can't just activate anything without knowing what is going on.
So yeah, maybe I'm not going to do it at all.
Or maybe I'm going to create my own timer.
You know what I'm going to do? Let's do something advanced, something a little bit fun.
I'm going to the ETC systemd
system directory and I'm going to
copy from user lib systemd system.
I'm going to copy HTTPD service to
here and I'm calling it Sander service.
Now what am I doing? Well, I'm going to build it up from scratch. I'm
creating my service and I want the service to be started by a timer.
Now I don't like finding out everything from scratch and that's
why I'm using this HTTP service as an example file.
And likewise I'm going to copy fstream
timer to colon slash Sander timer.
That's because if you want to run it through a timer then it's
on a first name basis and the first name basis here is sander.
So I am going to use sudo vim on Sander service to change my service. Well,
there's a lot that I can remove and I'm going to do all of that.
The only thing really that I need, I need a type for
my service and the type is going to be set to simple.
I need an exec start.
Everything else can go.
What is the exec start? Well, the exec start is going to be a
dummy command like user bin sleep infinity which no, not sleep infinity sleep 60.
So we are going to sleep 60 seconds every now and then.
So now I have my service, I'm going to create my timer.
So sudo vim standard timer and there I have my unit description.
I like it to be descriptive. So sonder timer and.
And the other stuff I don't need. And I have my timer section, I
can keep my timer section which is going to run the service every week.
And the only thing that I need to do right now, well first systemctl daemon reload because I
configured a lot of files on disk not using systemd so I do need my system daemon.
So I do need my systemctl daemon reload.
Now I need sudo systemctl enable timer and that is going to enable
the timer enable makes it will start well booting well, I also need
to start it now. So let's do a start and let's check out
using systemctl status on standard service what is going on?
And there you can see it's inactive but it will be triggered by the sonder timer. So
what? Have you just seen how easy it is to create your own systemd service files to
run any command and how easy it is to schedule them using a systemd timer?