In this lesson, you will learn how to create custom service configuration.
So in order to understand custom service configuration, you first need to understand unit file location
and priority.
Default units as provided from the packages are normally installed in userlib systemd
system.
There might be differences according to your distribution though.
Custom units should be installed in etc systemd system.
And there is a limited amount of auto-generated units which can be in the directory run systemd.
Run systemd is the highest priority, then comes etc systemd system, and then comes the
default unit.
There can even be user-specific units, and if a user has a user-specific unit, it can
be put in the .config/.systemd/.system directory in the user home directory.
Now if you want to edit your units, systemd is providing the commands to do so.
First you want to use systemctl cat to show current unit contents, and then you are going
to use systemctl edit to edit the unit and create drop-in files in etc systemd system
unit.d.
There's also an old way that can be used to change unit configuration, and the old way
is to copy the default unit file from userlib systemd system into etc systemd system, and
you provide your edits in there.
As etc systemd system has a higher priority, that one will win.
And if during a packet update your unit file gets updated, it will be written to userlib
systemd system, so your customizations will survive.
But this is not recommended.
It's the way how it was done in the beginning of systemd.
The recommended way is to use systemctl edit.
Let me show you what happens if you are using systemctl edit.
So to start with, I'm going to use systemctl cat on sshd.service, because I happen to know
that the sshd.service unit has an interesting option.
And here we can see the unit file configuration in which the unit section is where you define
your dependencies.
Look, here we have an after, which is telling that this should happen after the network
has been started.
And we have some wants.
The wants include information about all the units that need to be started when this one
is started.
The most important one is service, where you have all the different unit properties.
And I like this restart.
Restart is on failure, and restart sec is 42 seconds, is making sure that if anything
bad is happening to this unit, it will be started again.
So I'm going to copy that.
Then we also have an install section.
This install section is information that you need when you use systemctl enable.
Because systemctl enable needs to create a link in the systemd target wants directory.
And here you identify the systemd target wants directory.
OK.
I'm going to use systemctl edit httpd.service.
And what do we get?
Well, first, we are getting nano.
It happens to be that I don't like nano.
So I'm getting out of here, and I want to fix it.
Using export systemd editor is user bin vim.
And now if I use my systemctl edit, the same thing is happening, but this time in vim editor.
In the lower part of the screen, you can see that it is creating etc systemd system, httpd.service.d.
And that is how it works.
This is how it creates a drop-in file.
This drop-in file will be a part of the configuration, and it will have the name override.conf.
And anything that you put at the position where the cursor is right now will be included.
Now you need to help systemd a little bit, and you need to identify the section.
Well, I want my stuff to appear in the service section, and I copied it already.
Restart is on failure, and restart sec is 21 seconds.
That sounds like an amazing feature.
So I'm saving my changes.
And as I'm happy with this, I'm also going to use echo, Ctrl-A to move my cursor to the
start of the line, and then echo, export, and I want to have that in the tilde slash
dot bashrc.
It will make it persistent.
Good.
Now I'm going to use systemctl cat, httpd.service.
And in case you are wondering, don't we need to restart anything?
Well, let's figure it out.
Here, systemctl cat is giving me the contents of userlib systemd system.
Doesn't fit on one screen.
If I use uppercase D to go all the way to the bottom, look at that.
Here we have the drop-in file.
etc systemd system, httpd.service.d.
And if we use systemctl status on httpd, then what do we see?
We see it considers this drop-in file as well.
And that means that my httpd service is currently using the specific auto-restart option.
So let's check it out.
Let's use kill-9143335 to simulate a disaster that is happening on my process.
Then let's use systemctl status.
And systemctl status is giving me activating.
So this is how you can add configuration to your units.
And at the same time, this is how you can use this auto-restart feature, which is quite awesome.