Systemd for service management
Tags: linux, systemd, sysadmin
Published: 2020-06-02 11:55:24

{% youtube PC0nwmppb7U %}

# LinuxCast 01 - Managing services with systemd

`systemd` is possibly the most common init system you'll come across on Linux,
it's pre-installed on Ubuntu, Arch, Fedora and many more popular distributions.

It's also incredibly trivial to manage your services with it, and I'm going to
show you how to do just that in this inaugural LinuxCast.

So without further ado, let's jump in.

---

Starting and stopping services is probably the most common task you'll perform
using systemd. And it's incredibly easy.

On this computer, I'm running Arch Linux, and I have the containerization tool
'Docker' installed. It's a wonderful tool, but it doesn't automatically start
up. We can start it ourselves very easily and have it run in the background
using the power of systemd.

The command you use to do any task with systemd is `systemctl`, standing for
"system control", [not cuttlefish](https://bbs.archlinux.org/viewtopic.php?pid=1584556#p1584556). Who could've seen that coming?

In this instance, we want to *start* a service, so we're going to pop open a
terminal, and we'll type the following.

```zsh
# Start up the Docker service
$ sudo systemctl start docker.service
```

Now, if all has gone well, you'll notice that there wasn't any output. That's
good. So if we now run `docker ps`, we should see that familiar table from
Docker, just as expected.

---

But what if we realise it's taking up a ton of RAM, so we want to *stop* the
Docker service? For that, there's a very similar subcommand -- `stop`.

```zsh
# Stop the Docker service
$ sudo systemctl stop docker.service
```

And now, again, notice how `systemctl` gave us no output to work with. Again,
that's what we were expecting. It means that Docker has now stopped. So if we
run `docker ps` again, we should find that the client fails to connect to the
Docker socket.

---

What if you forgot whether or not you started a service or not? That's easy
too.

To do that, we want to view the `status` of the service. Can you guess the
command for that yet?

```shell
# Check the status of the Docker service
$ sudo systemctl status docker.service
```

Look at that, we have a lot of command-line output. The only thing we're
interested in, however, is the line that says "Active", and we can see that the
Docker service is "inactive" or "dead", meaning the service isn't running right
now.

---

Now, what about starting services when your computer boots up? Well, to
control whether services do that or not, we can use the `enable` and `disable`
`systemctl` commands.

Let's say I want Docker to start when my computer does. That's easily done.

```shell
# Tell systemd to start Docker on boot
$ sudo systemctl enable docker.service
```

This time round, we get output. Systemd is telling us that it's created
a symlink from the service file for Docker into the directory containing all
the services that are run on startup.

What about if I want to stop Docker starting when my computer does? Again,
easily done. It's as easy as using that `disable` command I mentioned earlier.

```shell
# Tell systemd to stop running Docker on boot
$ sudo systemctl disable docker.service
```

And we can see it's removed the docker service from that folder from earlier.

Now Docker will no longer start when our computer does.

---

Thanks for giving this a read. Drop me a follow if you found it useful, I'll be writing some more of these.