1
00:00:00,000 --> 00:00:11,360
In this lesson, you will learn how to create custom service configuration.

2
00:00:11,360 --> 00:00:17,480
So in order to understand custom service configuration, you first need to understand unit file location

3
00:00:17,480 --> 00:00:19,600
and priority.

4
00:00:19,600 --> 00:00:24,480
Default units as provided from the packages are normally installed in userlib systemd

5
00:00:24,480 --> 00:00:25,480
system.

6
00:00:25,480 --> 00:00:29,760
There might be differences according to your distribution though.

7
00:00:29,760 --> 00:00:33,980
Custom units should be installed in etc systemd system.

8
00:00:33,980 --> 00:00:40,680
And there is a limited amount of auto-generated units which can be in the directory run systemd.

9
00:00:40,680 --> 00:00:45,560
Run systemd is the highest priority, then comes etc systemd system, and then comes the

10
00:00:45,560 --> 00:00:48,799
default unit.

11
00:00:48,799 --> 00:00:53,560
There can even be user-specific units, and if a user has a user-specific unit, it can

12
00:00:53,560 --> 00:01:00,520
be put in the .config/.systemd/.system directory in the user home directory.

13
00:01:00,520 --> 00:01:06,120
Now if you want to edit your units, systemd is providing the commands to do so.

14
00:01:06,120 --> 00:01:10,760
First you want to use systemctl cat to show current unit contents, and then you are going

15
00:01:10,760 --> 00:01:17,680
to use systemctl edit to edit the unit and create drop-in files in etc systemd system

16
00:01:17,680 --> 00:01:18,680
unit.d.

17
00:01:19,599 --> 00:01:24,599
There's also an old way that can be used to change unit configuration, and the old way

18
00:01:24,599 --> 00:01:30,720
is to copy the default unit file from userlib systemd system into etc systemd system, and

19
00:01:30,720 --> 00:01:32,919
you provide your edits in there.

20
00:01:32,919 --> 00:01:37,279
As etc systemd system has a higher priority, that one will win.

21
00:01:37,279 --> 00:01:43,279
And if during a packet update your unit file gets updated, it will be written to userlib

22
00:01:43,279 --> 00:01:48,360
systemd system, so your customizations will survive.

23
00:01:49,040 --> 00:01:50,040
But this is not recommended.

24
00:01:50,040 --> 00:01:52,839
It's the way how it was done in the beginning of systemd.

25
00:01:52,839 --> 00:01:57,000
The recommended way is to use systemctl edit.

26
00:01:57,000 --> 00:02:01,480
Let me show you what happens if you are using systemctl edit.

27
00:02:01,480 --> 00:02:07,599
So to start with, I'm going to use systemctl cat on sshd.service, because I happen to know

28
00:02:07,599 --> 00:02:12,360
that the sshd.service unit has an interesting option.

29
00:02:12,360 --> 00:02:17,600
And here we can see the unit file configuration in which the unit section is where you define

30
00:02:17,600 --> 00:02:18,600
your dependencies.

31
00:02:18,600 --> 00:02:22,520
Look, here we have an after, which is telling that this should happen after the network

32
00:02:22,520 --> 00:02:24,080
has been started.

33
00:02:24,080 --> 00:02:25,639
And we have some wants.

34
00:02:25,639 --> 00:02:30,320
The wants include information about other units that need to be started when this one

35
00:02:30,320 --> 00:02:32,039
is started.

36
00:02:32,039 --> 00:02:36,440
The most important one is service, where you have all the different unit properties.

37
00:02:36,440 --> 00:02:38,360
And I like this restart.

38
00:02:38,360 --> 00:02:43,360
Restart is on failure and restart sec is 42 seconds, is making sure that if anything bad

39
00:02:43,360 --> 00:02:46,720
is happening to this unit, it will be started again.

40
00:02:46,720 --> 00:02:48,839
So I'm going to copy that.

41
00:02:48,839 --> 00:02:50,600
Then we also have an install section.

42
00:02:50,600 --> 00:02:56,399
This install section is information that you need when you use systemctl enable.

43
00:02:56,399 --> 00:03:03,080
Because systemctl enable needs to create a link in the systemd target wants directory.

44
00:03:03,080 --> 00:03:06,559
And here you identify the systemd target wants directory.

45
00:03:06,559 --> 00:03:07,559
OK.

46
00:03:07,559 --> 00:03:13,600
I'm going to use systemctl edit httpd.service.

47
00:03:13,600 --> 00:03:14,600
And what do we get?

48
00:03:14,600 --> 00:03:16,440
Well, first, we are getting nano.

49
00:03:17,160 --> 00:03:18,160
It happens to be that I don't like nano.

50
00:03:18,160 --> 00:03:22,039
So I'm getting out of here and I want to fix it.

51
00:03:22,039 --> 00:03:31,479
Using export systemd editor is user bin vim.

52
00:03:31,479 --> 00:03:37,759
And now if I use my systemctl edit, the same thing is happening, but this time in vim editor.

53
00:03:37,759 --> 00:03:45,039
In the lower part of the screen, you can see that it is creating etc systemd system, httpd.service.d.

54
00:03:45,039 --> 00:03:46,399
And that is how it works.

55
00:03:46,399 --> 00:03:49,479
This is how it creates a drop-in file.

56
00:03:49,479 --> 00:03:55,479
This drop-in file will be a part of the configuration, and it will have the name override.conf.

57
00:03:55,479 --> 00:04:02,919
And anything that you put at the position where the cursor is right now will be included.

58
00:04:02,919 --> 00:04:06,919
Now you need to help systemd a little bit, and you need to identify the section.

59
00:04:06,919 --> 00:04:13,080
Well, I want my stuff to appear in the service section, and I copied it already.

60
00:04:13,080 --> 00:04:17,000
Restart is on failure and restart sec is 21 seconds.

61
00:04:17,000 --> 00:04:19,679
That sounds like an amazing feature.

62
00:04:19,679 --> 00:04:22,600
So I'm saving my changes.

63
00:04:22,600 --> 00:04:32,399
And as I'm happy with this, I'm also going to use echo, Ctrl-A to move my cursor to the

64
00:04:32,399 --> 00:04:36,359
start of the line, and then echo export.

65
00:04:36,359 --> 00:04:43,200
And I want to have that in d tilde slash dot bash rc.

66
00:04:43,200 --> 00:04:44,600
It will make it persistent.

67
00:04:44,600 --> 00:04:45,600
Good.

68
00:04:45,600 --> 00:04:50,880
Now I'm going to use systemctl cat, httpd.service.

69
00:04:50,880 --> 00:04:53,920
And in case you are wondering, don't we need to restart anything?

70
00:04:53,920 --> 00:04:56,160
Well, let's figure it out.

71
00:04:56,160 --> 00:05:01,239
Here systemctl cat is giving me the contents of userlib systemd system.

72
00:05:01,239 --> 00:05:02,239
Doesn't fit on one screen.

73
00:05:02,359 --> 00:05:06,399
If I use uppercase G to go all the way to the bottom, look at that.

74
00:05:06,399 --> 00:05:13,839
Here we have the drop-in file, etc systemd system, httpd.service.d.

75
00:05:13,839 --> 00:05:20,559
And if we use systemctl status on httpd, then what do we see?

76
00:05:20,559 --> 00:05:24,600
We see it considers this drop-in file as well.

77
00:05:24,600 --> 00:05:31,399
And that means that my httpd service is currently using the specific auto-restart option.

78
00:05:31,559 --> 00:05:33,000
So let's check it out.

79
00:05:33,000 --> 00:05:41,880
Let's use kill minus 9, 1, 4, 3, 3, 3, 5, to simulate a disaster that is happening on

80
00:05:41,880 --> 00:05:43,399
my process.

81
00:05:43,399 --> 00:05:46,480
Then let's use systemctl status.

82
00:05:46,480 --> 00:05:49,959
And systemctl status is giving me activating.

83
00:05:49,959 --> 00:05:53,640
So this is how you can add configuration to your units.

84
00:05:53,640 --> 00:05:58,160
And at the same time, this is how you can use this auto-restart feature, which is quite

85
00:05:58,160 --> 00:05:58,679
awesome.

