In this video, we'll look at service management using systemd and
Linux.
Our plan is to explore how Linux manages processes and
services using system d.
We will take a look at a system B unit file and finally you
will use this system control command to manage the lifecycle
of a service.
Okay, let's get started.
Okay, so back in the terminal once again and
One thing you need to know about modern Linux systems is that
they use the system D Suite of tools. So systemd
is a process manager and more
and it it is the first process started
when Linux boots up.
It basically controls how all services and processes are
managed inside Linux system D
manages work using unit files. So basically
a unit files a configuration file that describes how
to handle like a process a socket or whatever
you want or need some need to manage.
There are multiple unit categories, but we're going to
focus on the service type for this video. Okay, so I
think the best way to understand what a unit a unit
file is is actually by taking a look at one.
So we're gonna do is we're gonna look at one for the postfix mail
server.
And so the path where this is is kind of long so
I will type it out and then explain.
okay, so this directory or
sorry this unit file is kept in a
system d sub directory called multi-user
Target wants
And that is where this file is
being stalled there are different ones, but it's doesn't really
matter for this example.
This file as you can see is called postfix.service and
is because it is a service type.
This so unit files have different type of categories. And like
I said, we're gonna talk just about the the service one here. So you
can see this file is split up into three different
sections. We have a unit section a service section and install
the unit section. So this is generally used for divining
for defining metadata for the unit and
configuring the relationship of the unit to like other units. For
example, you have a description here and we says
that it's the most it's the postfix mail transfer agent.
You have an after declaration here. And so
what this means is that this unit should be
started after the network Target and
syslog because mail server
needs to write to a log and the mail server needs Network up
so it can accept connections. So that makes sense it conflicts
with the send mail service in the
exam service. So
It will not start those other two if this
it will not start this unit, if one of those other two already started,
okay, so next is the service. So the
service declaration here defines
the configuration of what
type of Service Unit it is here. We have
how to run the application and what params it
needs like for example, this service is type forking means that
once the parrot process starts a
child will be immediately created and
when that the parent process will
go away but the child will still run and that just tells system
need like hey if the parent process goes away, there's still
a child running don't shut it down.
Here's the PID file and this is what we use obviously for
the the process ID file for the
actual service. Once it's running you have here anvironment file
and these are just you could set if
you have different environment variables. You want to set the exact start
pre you can see this two of them here and you
could put as many as you want but these are commands to run before you
actually start the process the application
in this case. It's the exact start exec start
actually starts postfix and you can
see right here just runs the postfix command and passes the
start argument to it. The reload does the same thing. So
if you ever want to reload you using
the system CTL command your own reload
a unit you can do it that way and if you
want if you stop it, this is the command it knows how
to stop it.
Okay. So next is the install so install defines
the behavior if it's enabled. So if you have this actually in
a unit, you know that if it has
this declaration that this unit file can be enabled there are
some that that are not enabled by at all,
and we won't really discuss them here. But just to
know if you see this this means it can be enabled.
And so wanted by here in this context. It says
that when it is enabled right put this
unit file inside a directory called
the multi-user.target, okay.
So there is the basics of unit file. Obviously, they
can get way more complex than this. So please
I encourage you
to learn more outside of this course as you
will say as as administering a Lennox
host. There's a lot of systemd knowledge that
you will want to know.
Okay, I'm gonna clear the screen here.
So if you wanted to see all the unit files
that that are on a particular host you
can use the system CTL command and
this is the command we're gonna use the most probably going forward.
But so we can do system.
CTL
and then we're going to say a list unit files.
And what this is going to do is going to list all the unit files and
let us know if they're enabled or not enabled and there'll be
a lot in the screen here. So I'll just go ahead and return.
And this does a paging mechanism so
you can see where lines one of 40 and you can see
here like for example.
Brand bot dot path that is enabled
ARP ethers is disabled and so you
can go through and in fact if you wanted to find our postfix.
You could do a slash post fix while you're in this paging and
you can see the postfix.services enabled which
you know, not a surprise here. So you hit the
letter Q to get out and I just wanted you to so you
could go through and see all the ones that were enabled or disabled. So
I'm gonna clear the screen once again.
so
We're gonna talk about now, which we're like
systemctl which is the main command line
application that interacts with systemd. We've touched
a little bit here. But now let's go ahead and focus on
the commands that you're gonna most
likely use the most
like with all the stuff we've talked about here. There's a
lot more stuff you could do with system CTL a lot more subcommands,
but
I've Cherry Picked these ones because I think these are the ones that will help
you build the best foundation.
Okay. So the ones we're going to focus on for today
are status.
Start stop enable and
disable.
So status gives us the status of
the unit like we did early on the earlier video when we
looked at the network service. It'll let you know that it's loaded if
which means it's enabled and it's active and that's what
status will give us.
Enable will make the unit available to be run and
they'll be run on on boot and we
saw that earlier in the unit file when we had that install section.
Disabled does the exact opposite it will remove
it from those system directories. Whereas
social system D will not actually activate
them or try to run them.
So the start obviously starts the
whatever the unit file is and in our case we're talking
about a service. So it'll start the service and stop obviously
does the opposite it'll stop the service
since we look at the postfix. I thought we'd keep that going and let's
go ahead and look at the actual status.
Of postfix. So what we do is we're gonna
type system CTL.
this and then status is the subcommand and then the unit
we want to look at in our case is postfix dot
service and you can tell the dot whatever
it is is the category of the unit file and like I said,
we're just really looking at services so
So, alright. So here we go. This should look a little bit familiar like from the
network that we looked at earlier.
And as you can see, it's the postfix service we can see it's loaded.
It's enabled it gives you right here.
It's active and it's running and it tells
you what how long it's been running since and for this for this
particular thing. It's been running for a day in three hours.
And as we saw in that unit file, we saw the exact start
and the exact Prez that are run and you
know, we call the postfix command with the start argument to
it. This gives us the actual output
of all the rest of the commands that are actually run through
the pids and then down here.
We get a little bonus here. We get a little bit of logs. So
we get some activity that we see.
From the logs of postfix not all of them, but just
a small little snippet and as you can tell this is
the beginning of the log when it started because we could see right here that starting
postfix.
And then it starting to post
six Mail system and the rest of the logs and started the agent as
well.
So that is the status of our postfix
server.
So the enable obviously like I
said will make it start and run.
We are not going to obviously disable this or enable it
you got to get the idea. It basically just removes this
file from that directory. We looked at earlier and it
enable it puts it back in in system D picks that
back up.
Okay. So the next thing we should do here is the stop and start.
So let me clear the screen.
And so here we're gonna need elevated privileges again. So anytime
that you start and stop a anytime you start
and stop a service especially that binds
to a poor like this you will need elevated privileges so you can
do sudo system CTL and then
we're going to use the stop.
And we're gonna do.
postfix.service
So nothing return means that
system D systemctl is best
guesses that the postfix server stopped one way
to test is we can go back up to our status command.
And now as you can see, it's still
loaded because it is in an enabled unit. But
now you can see that it's inactive it's dead and
it it was killed 12 seconds ago, which is about
when we stopped it, right and then you can see here in
the logs. Now, we can see stopping the postfix mail
transfer agent.
Okay, so
excitingly next we're gonna start it back up and instead
of the stop. We're gonna use the
start command subcommand.
And no results because it thinks
it's best guess that it actually started and then let's run
status. And here we are again, we can see that it's
active and it's been running for five seconds.
And we can see that in the logs. Once again it started.
Okay, so that is the brief overview
of how to manage services or processes or
applications inside your Linux host using system
D and most importantly using
the system CTL command lineup. Okay, let's
jump back and let's do a recap of what we did.
Okay. So let's talk about we did in this video you explored system
D and how it is used on a Linux host.
You took a look at a Service Unit file and explored all
the sections that made it up.
You use the system CTL command to manage the postfix service.
In the next video, you will configure and run a Maria
database inside a container. See you
soon.