DEV Community

Cover image for Systemd for service management

Systemd for service management

Hayden Young
I'm a full-stack developer adept in JavaScript/TypeScript and React, learning Go and C# on the side. My work is in PHP currently, though.
・3 min read

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. 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.

# Start up the Docker service
$ sudo systemctl start docker.service
Enter fullscreen mode Exit fullscreen mode

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.

# Stop the Docker service
$ sudo systemctl stop docker.service
Enter fullscreen mode Exit fullscreen mode

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

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

# Check the status of the Docker service
$ sudo systemctl status docker.service
Enter fullscreen mode Exit fullscreen mode

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, 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.

# Tell systemd to start Docker on boot
$ sudo systemctl enable docker.service
Enter fullscreen mode Exit fullscreen mode

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.

# Tell systemd to stop running Docker on boot
$ sudo systemctl disable docker.service
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (1)

karx1 profile image
Yash Karandikar

For .service files, you can leave out the .service, because it is assumed by default.

sudo systemctl start docker.service


sudo systemctl start docker

both start the same service.