WEBVTT - This file was automatically generated by VIMEO

0
00:00:07.100 --> 00:00:10.500
In this video, we'll look at service management using systemd and

1
00:00:10.500 --> 00:00:10.700
 Linux.

2
00:00:11.700 --> 00:00:14.800
Our plan is to explore how Linux manages processes and

3
00:00:14.800 --> 00:00:16.300
 services using system d.

4
00:00:17.300 --> 00:00:20.700
We will take a look at a system B unit file and finally you

5
00:00:20.700 --> 00:00:23.500
 will use this system control command to manage the lifecycle

6
00:00:23.500 --> 00:00:24.000
 of a service.

7
00:00:24.700 --> 00:00:25.900
Okay, let's get started.

8
00:00:27.200 --> 00:00:30.300
Okay, so back in the terminal once again and

9
00:00:31.400 --> 00:00:34.100
One thing you need to know about modern Linux systems is that

10
00:00:34.100 --> 00:00:38.400
 they use the system D Suite of tools. So systemd

11
00:00:37.400 --> 00:00:40.700
 is a process manager and more

12
00:00:40.700 --> 00:00:43.700
 and it it is the first process started

13
00:00:43.700 --> 00:00:44.400
 when Linux boots up.

14
00:00:45.500 --> 00:00:48.500
It basically controls how all services and processes are

15
00:00:48.500 --> 00:00:51.200
 managed inside Linux system D

16
00:00:51.200 --> 00:00:54.800
 manages work using unit files. So basically

17
00:00:54.800 --> 00:00:57.400
 a unit files a configuration file that describes how

18
00:00:57.400 --> 00:01:00.500
 to handle like a process a socket or whatever

19
00:01:00.500 --> 00:01:02.600
 you want or need some need to manage.

20
00:01:03.300 --> 00:01:06.200
There are multiple unit categories, but we're going to

21
00:01:06.200 --> 00:01:09.800
 focus on the service type for this video. Okay, so I

22
00:01:09.800 --> 00:01:12.900
 think the best way to understand what a unit a unit

23
00:01:12.900 --> 00:01:14.600
 file is is actually by taking a look at one.

24
00:01:15.200 --> 00:01:18.400
So we're gonna do is we're gonna look at one for the postfix mail

25
00:01:18.400 --> 00:01:18.800
 server.

26
00:01:19.300 --> 00:01:22.500
And so the path where this is is kind of long so

27
00:01:22.500 --> 00:01:25.000
 I will type it out and then explain.

28
00:01:35.400 --> 00:01:38.500
okay, so this directory or

29
00:01:38.500 --> 00:01:40.700
 sorry this unit file is kept in a

30
00:01:41.700 --> 00:01:45.300
system d sub directory called multi-user

31
00:01:44.300 --> 00:01:45.900
 Target wants

32
00:01:46.600 --> 00:01:49.300
And that is where this file is

33
00:01:49.300 --> 00:01:52.700
 being stalled there are different ones, but it's doesn't really

34
00:01:52.700 --> 00:01:53.800
 matter for this example.

35
00:01:54.700 --> 00:01:58.300
This file as you can see is called postfix.service and

36
00:01:57.300 --> 00:02:00.500
 is because it is a service type.

37
00:02:01.200 --> 00:02:04.700
This so unit files have different type of categories. And like

38
00:02:04.700 --> 00:02:07.800
 I said, we're gonna talk just about the the service one here. So you

39
00:02:07.800 --> 00:02:10.200
 can see this file is split up into three different

40
00:02:10.200 --> 00:02:13.700
 sections. We have a unit section a service section and install

41
00:02:14.800 --> 00:02:17.600
the unit section. So this is generally used for divining

42
00:02:17.600 --> 00:02:20.400
 for defining metadata for the unit and

43
00:02:20.400 --> 00:02:24.100
 configuring the relationship of the unit to like other units. For

44
00:02:23.100 --> 00:02:26.200
 example, you have a description here and we says

45
00:02:26.200 --> 00:02:29.200
 that it's the most it's the postfix mail transfer agent.

46
00:02:29.800 --> 00:02:32.300
You have an after declaration here. And so

47
00:02:32.300 --> 00:02:35.400
 what this means is that this unit should be

48
00:02:35.400 --> 00:02:39.200
 started after the network Target and

49
00:02:38.200 --> 00:02:41.800
 syslog because mail server

50
00:02:41.800 --> 00:02:44.300
 needs to write to a log and the mail server needs Network up

51
00:02:44.300 --> 00:02:48.400
 so it can accept connections. So that makes sense it conflicts

52
00:02:47.400 --> 00:02:50.000
 with the send mail service in the

53
00:02:50.800 --> 00:02:51.100
 exam service. So

54
00:02:51.800 --> 00:02:54.400
It will not start those other two if this

55
00:02:54.400 --> 00:02:57.700
 it will not start this unit, if one of those other two already started,

56
00:02:57.700 --> 00:03:01.400
 okay, so next is the service. So the

57
00:03:00.400 --> 00:03:04.600
 service declaration here defines

58
00:03:03.600 --> 00:03:06.300
 the configuration of what

59
00:03:06.300 --> 00:03:09.200
 type of Service Unit it is here. We have

60
00:03:09.200 --> 00:03:12.200
 how to run the application and what params it

61
00:03:12.200 --> 00:03:15.200
 needs like for example, this service is type forking means that

62
00:03:15.200 --> 00:03:18.700
 once the parrot process starts a

63
00:03:18.700 --> 00:03:21.700
 child will be immediately created and

64
00:03:21.700 --> 00:03:24.500
 when that the parent process will

65
00:03:24.500 --> 00:03:27.400
 go away but the child will still run and that just tells system

66
00:03:27.400 --> 00:03:30.200
 need like hey if the parent process goes away, there's still

67
00:03:30.200 --> 00:03:31.900
 a child running don't shut it down.

68
00:03:32.800 --> 00:03:35.700
Here's the PID file and this is what we use obviously for

69
00:03:35.700 --> 00:03:38.100
 the the process ID file for the

70
00:03:38.100 --> 00:03:41.500
 actual service. Once it's running you have here anvironment file

71
00:03:41.500 --> 00:03:44.300
 and these are just you could set if

72
00:03:44.300 --> 00:03:47.700
 you have different environment variables. You want to set the exact start

73
00:03:47.700 --> 00:03:50.300
 pre you can see this two of them here and you

74
00:03:50.300 --> 00:03:53.800
 could put as many as you want but these are commands to run before you

75
00:03:53.800 --> 00:03:56.200
 actually start the process the application

76
00:03:56.200 --> 00:03:59.900
 in this case. It's the exact start exec start

77
00:03:59.900 --> 00:04:02.000
 actually starts postfix and you can

78
00:04:02.200 --> 00:04:05.800
 see right here just runs the postfix command and passes the

79
00:04:05.800 --> 00:04:08.600
 start argument to it. The reload does the same thing. So

80
00:04:08.600 --> 00:04:11.800
 if you ever want to reload you using

81
00:04:11.800 --> 00:04:14.600
 the system CTL command your own reload

82
00:04:14.600 --> 00:04:17.200
 a unit you can do it that way and if you

83
00:04:17.200 --> 00:04:20.500
 want if you stop it, this is the command it knows how

84
00:04:20.500 --> 00:04:21.100
 to stop it.

85
00:04:21.800 --> 00:04:24.900
Okay. So next is the install so install defines

86
00:04:24.900 --> 00:04:27.700
 the behavior if it's enabled. So if you have this actually in

87
00:04:27.700 --> 00:04:30.100
 a unit, you know that if it has

88
00:04:30.100 --> 00:04:34.100
 this declaration that this unit file can be enabled there are

89
00:04:33.100 --> 00:04:36.500
 some that that are not enabled by at all,

90
00:04:36.500 --> 00:04:39.600
 and we won't really discuss them here. But just to

91
00:04:39.600 --> 00:04:41.300
 know if you see this this means it can be enabled.

92
00:04:42.100 --> 00:04:45.200
And so wanted by here in this context. It says

93
00:04:45.200 --> 00:04:48.600
 that when it is enabled right put this

94
00:04:48.600 --> 00:04:51.200
 unit file inside a directory called

95
00:04:51.200 --> 00:04:53.400
 the multi-user.target, okay.

96
00:04:54.300 --> 00:04:57.900
So there is the basics of unit file. Obviously, they

97
00:04:57.900 --> 00:05:01.400
 can get way more complex than this. So please

98
00:05:00.400 --> 00:05:03.500
 I encourage you

99
00:05:03.500 --> 00:05:07.100
 to learn more outside of this course as you

100
00:05:06.100 --> 00:05:09.700
 will say as as administering a Lennox

101
00:05:09.700 --> 00:05:12.300
 host. There's a lot of systemd knowledge that

102
00:05:12.300 --> 00:05:13.100
 you will want to know.

103
00:05:14.300 --> 00:05:15.800
Okay, I'm gonna clear the screen here.

104
00:05:16.800 --> 00:05:19.800
So if you wanted to see all the unit files

105
00:05:19.800 --> 00:05:22.500
 that that are on a particular host you

106
00:05:22.500 --> 00:05:25.600
 can use the system CTL command and

107
00:05:25.600 --> 00:05:28.100
 this is the command we're gonna use the most probably going forward.

108
00:05:28.800 --> 00:05:30.400
But so we can do system.

109
00:05:31.500 --> 00:05:32.200
CTL

110
00:05:32.900 --> 00:05:36.100
and then we're going to say a list unit files.

111
00:05:36.800 --> 00:05:39.100
And what this is going to do is going to list all the unit files and

112
00:05:39.100 --> 00:05:42.000
 let us know if they're enabled or not enabled and there'll be

113
00:05:42.200 --> 00:05:44.100
 a lot in the screen here. So I'll just go ahead and return.

114
00:05:45.200 --> 00:05:48.500
And this does a paging mechanism so

115
00:05:48.500 --> 00:05:51.200
 you can see where lines one of 40 and you can see

116
00:05:51.200 --> 00:05:52.300
 here like for example.

117
00:05:53.800 --> 00:05:56.400
Brand bot dot path that is enabled

118
00:05:56.400 --> 00:05:59.200
 ARP ethers is disabled and so you

119
00:05:59.200 --> 00:06:01.900
 can go through and in fact if you wanted to find our postfix.

120
00:06:03.400 --> 00:06:06.800
You could do a slash post fix while you're in this paging and

121
00:06:06.800 --> 00:06:09.100
 you can see the postfix.services enabled which

122
00:06:09.100 --> 00:06:12.100
 you know, not a surprise here. So you hit the

123
00:06:12.100 --> 00:06:15.400
 letter Q to get out and I just wanted you to so you

124
00:06:15.400 --> 00:06:18.500
 could go through and see all the ones that were enabled or disabled. So

125
00:06:18.500 --> 00:06:19.700
 I'm gonna clear the screen once again.

126
00:06:21.400 --> 00:06:21.600
so

127
00:06:22.700 --> 00:06:25.900
We're gonna talk about now, which we're like

128
00:06:25.900 --> 00:06:29.000
 systemctl which is the main command line

129
00:06:29.600 --> 00:06:32.500
 application that interacts with systemd. We've touched

130
00:06:32.500 --> 00:06:35.200
 a little bit here. But now let's go ahead and focus on

131
00:06:35.200 --> 00:06:38.600
 the commands that you're gonna most

132
00:06:38.600 --> 00:06:39.500
 likely use the most

133
00:06:40.400 --> 00:06:43.100
like with all the stuff we've talked about here. There's a

134
00:06:43.100 --> 00:06:47.100
 lot more stuff you could do with system CTL a lot more subcommands,

135
00:06:46.100 --> 00:06:47.200
 but

136
00:06:48.300 --> 00:06:51.200
I've Cherry Picked these ones because I think these are the ones that will help

137
00:06:51.200 --> 00:06:52.200
 you build the best foundation.

138
00:06:53.200 --> 00:06:56.100
Okay. So the ones we're going to focus on for today

139
00:06:56.100 --> 00:06:57.100
 are status.

140
00:06:57.900 --> 00:07:00.300
Start stop enable and

141
00:07:00.300 --> 00:07:00.800
 disable.

142
00:07:01.500 --> 00:07:04.200
So status gives us the status of

143
00:07:04.200 --> 00:07:07.100
 the unit like we did early on the earlier video when we

144
00:07:07.100 --> 00:07:10.400
 looked at the network service. It'll let you know that it's loaded if

145
00:07:10.400 --> 00:07:13.500
 which means it's enabled and it's active and that's what

146
00:07:13.500 --> 00:07:14.400
 status will give us.

147
00:07:15.200 --> 00:07:19.100
Enable will make the unit available to be run and

148
00:07:18.100 --> 00:07:21.500
 they'll be run on on boot and we

149
00:07:21.500 --> 00:07:24.100
 saw that earlier in the unit file when we had that install section.

150
00:07:24.900 --> 00:07:27.300
Disabled does the exact opposite it will remove

151
00:07:27.300 --> 00:07:31.500
 it from those system directories. Whereas

152
00:07:30.500 --> 00:07:33.900
 social system D will not actually activate

153
00:07:33.900 --> 00:07:34.900
 them or try to run them.

154
00:07:35.500 --> 00:07:38.400
So the start obviously starts the

155
00:07:38.400 --> 00:07:41.200
 whatever the unit file is and in our case we're talking

156
00:07:41.200 --> 00:07:45.500
 about a service. So it'll start the service and stop obviously

157
00:07:44.500 --> 00:07:47.400
 does the opposite it'll stop the service

158
00:07:47.400 --> 00:07:50.700
 since we look at the postfix. I thought we'd keep that going and let's

159
00:07:50.700 --> 00:07:52.700
 go ahead and look at the actual status.

160
00:07:53.900 --> 00:07:56.200
Of postfix. So what we do is we're gonna

161
00:07:56.200 --> 00:07:57.600
 type system CTL.

162
00:07:58.300 --> 00:08:01.800
this and then status is the subcommand and then the unit

163
00:08:01.800 --> 00:08:05.300
 we want to look at in our case is postfix dot

164
00:08:04.300 --> 00:08:07.700
 service and you can tell the dot whatever

165
00:08:07.700 --> 00:08:10.000
 it is is the category of the unit file and like I said,

166
00:08:10.100 --> 00:08:11.500
 we're just really looking at services so

167
00:08:12.300 --> 00:08:15.200
So, alright. So here we go. This should look a little bit familiar like from the

168
00:08:15.200 --> 00:08:16.600
 network that we looked at earlier.

169
00:08:17.300 --> 00:08:20.300
And as you can see, it's the postfix service we can see it's loaded.

170
00:08:20.300 --> 00:08:23.000
 It's enabled it gives you right here.

171
00:08:23.600 --> 00:08:26.400
It's active and it's running and it tells

172
00:08:26.400 --> 00:08:29.400
 you what how long it's been running since and for this for this

173
00:08:29.400 --> 00:08:32.300
 particular thing. It's been running for a day in three hours.

174
00:08:33.200 --> 00:08:36.800
And as we saw in that unit file, we saw the exact start

175
00:08:36.800 --> 00:08:39.700
 and the exact Prez that are run and you

176
00:08:39.700 --> 00:08:42.800
 know, we call the postfix command with the start argument to

177
00:08:42.800 --> 00:08:45.800
 it. This gives us the actual output

178
00:08:45.800 --> 00:08:48.700
 of all the rest of the commands that are actually run through

179
00:08:48.700 --> 00:08:50.500
 the pids and then down here.

180
00:08:51.500 --> 00:08:54.600
We get a little bonus here. We get a little bit of logs. So

181
00:08:54.600 --> 00:08:56.500
 we get some activity that we see.

182
00:08:57.600 --> 00:09:00.200
From the logs of postfix not all of them, but just

183
00:09:00.200 --> 00:09:03.500
 a small little snippet and as you can tell this is

184
00:09:03.500 --> 00:09:06.700
 the beginning of the log when it started because we could see right here that starting

185
00:09:06.700 --> 00:09:07.300
 postfix.

186
00:09:08.800 --> 00:09:11.200
And then it starting to post

187
00:09:11.200 --> 00:09:14.500
 six Mail system and the rest of the logs and started the agent as

188
00:09:14.500 --> 00:09:14.500
 well.

189
00:09:16.200 --> 00:09:19.800
So that is the status of our postfix

190
00:09:19.800 --> 00:09:20.100
 server.

191
00:09:20.800 --> 00:09:23.200
So the enable obviously like I

192
00:09:23.200 --> 00:09:25.500
 said will make it start and run.

193
00:09:26.600 --> 00:09:29.500
We are not going to obviously disable this or enable it

194
00:09:29.500 --> 00:09:32.300
 you got to get the idea. It basically just removes this

195
00:09:32.300 --> 00:09:35.500
 file from that directory. We looked at earlier and it

196
00:09:35.500 --> 00:09:38.100
 enable it puts it back in in system D picks that

197
00:09:38.100 --> 00:09:38.500
 back up.

198
00:09:39.300 --> 00:09:42.500
Okay. So the next thing we should do here is the stop and start.

199
00:09:42.500 --> 00:09:43.700
 So let me clear the screen.

200
00:09:44.400 --> 00:09:47.400
And so here we're gonna need elevated privileges again. So anytime

201
00:09:47.400 --> 00:09:50.200
 that you start and stop a anytime you start

202
00:09:50.200 --> 00:09:53.600
 and stop a service especially that binds

203
00:09:53.600 --> 00:09:56.500
 to a poor like this you will need elevated privileges so you can

204
00:09:56.500 --> 00:09:59.400
 do sudo system CTL and then

205
00:09:59.400 --> 00:10:00.500
 we're going to use the stop.

206
00:10:01.800 --> 00:10:02.600
And we're gonna do.

207
00:10:03.600 --> 00:10:04.800
postfix.service

208
00:10:05.800 --> 00:10:08.400
So nothing return means that

209
00:10:08.400 --> 00:10:11.600
 system D systemctl is best

210
00:10:11.600 --> 00:10:14.500
 guesses that the postfix server stopped one way

211
00:10:14.500 --> 00:10:16.800
 to test is we can go back up to our status command.

212
00:10:18.800 --> 00:10:21.800
And now as you can see, it's still

213
00:10:21.800 --> 00:10:24.600
 loaded because it is in an enabled unit. But

214
00:10:24.600 --> 00:10:27.700
 now you can see that it's inactive it's dead and

215
00:10:27.700 --> 00:10:30.600
 it it was killed 12 seconds ago, which is about

216
00:10:30.600 --> 00:10:34.100
 when we stopped it, right and then you can see here in

217
00:10:33.100 --> 00:10:36.700
 the logs. Now, we can see stopping the postfix mail

218
00:10:36.700 --> 00:10:37.600
 transfer agent.

219
00:10:38.200 --> 00:10:38.800
Okay, so

220
00:10:40.300 --> 00:10:45.000
excitingly next we're gonna start it back up and instead

221
00:10:43.200 --> 00:10:46.200
 of the stop. We're gonna use the

222
00:10:46.200 --> 00:10:47.800
 start command subcommand.

223
00:10:48.700 --> 00:10:51.300
And no results because it thinks

224
00:10:51.300 --> 00:10:54.100
 it's best guess that it actually started and then let's run

225
00:10:54.100 --> 00:10:57.300
 status. And here we are again, we can see that it's

226
00:10:57.300 --> 00:10:59.400
 active and it's been running for five seconds.

227
00:11:00.300 --> 00:11:03.500
And we can see that in the logs. Once again it started.

228
00:11:04.300 --> 00:11:07.800
Okay, so that is the brief overview

229
00:11:07.800 --> 00:11:10.800
 of how to manage services or processes or

230
00:11:10.800 --> 00:11:14.300
 applications inside your Linux host using system

231
00:11:13.300 --> 00:11:16.700
 D and most importantly using

232
00:11:16.700 --> 00:11:19.600
 the system CTL command lineup. Okay, let's

233
00:11:19.600 --> 00:11:21.600
 jump back and let's do a recap of what we did.

234
00:11:22.400 --> 00:11:25.700
Okay. So let's talk about we did in this video you explored system

235
00:11:25.700 --> 00:11:27.800
 D and how it is used on a Linux host.

236
00:11:28.500 --> 00:11:31.300
You took a look at a Service Unit file and explored all

237
00:11:31.300 --> 00:11:32.400
 the sections that made it up.

238
00:11:33.200 --> 00:11:36.600
You use the system CTL command to manage the postfix service.

239
00:11:38.600 --> 00:11:41.700
In the next video, you will configure and run a Maria

240
00:11:41.700 --> 00:11:44.400
 database inside a container. See you

241
00:11:44.400 --> 00:11:44.400
 soon.
