Service Management with systemd
local graph
Chapter 2 — Service Management with systemd
Section titled “Chapter 2 — Service Management with systemd”In the previous chapter, you learned about how processes work in Linux. Now it’s time to look at how these processes are wrapped in an additional layer of abstraction: the systemd service.
The commands you’ve seen so far – ls, mv, rm, ps, and others – run in the foreground, attached to your shell session. You run them, the programs do their job, and then they exit. However, not all programs run like this.
Services, also frequently called daemons, are long-running processes that run in the background. These can be things like databases and web servers, but also regular system services like your network manager, your desktop environment, and so on. These long-running background services are typically started and controlled via an init system such as systemd.
init here refers to the first process your operating system kernel starts, and it is the job of this process to take care of starting any other processes.
systemd services are controlled using a command line utility called systemctl. It will be used for starting and stopping services, for example, to restart a service that’s misbehaving or to reload one whose configuration has changed.
If you’re bouncing around the book and haven’t read the previous chapter yet, you can still get value out of this one. For now, just think of a process as any running command, application, or service. When you’re ready to learn about how processes work in more detail, you can read Chapter 2, Working with Processes.
In this chapter, you’ll learn all about:
- The command you’ll use to interact with
systemdservices:systemctl - A slightly deeper dive into what an init system does, and how
systemdspecifically fills this role - Managing services with
systemctl - A few tips for working in container environments (like Docker containers), which usually don’t have the kind of robust service management layer we describe in this chapter
Note
This chapter applies to Linux only – macOS and Windows (and even other Unixes) manage processes using different tools. In fact, different Linux distributions use different tools, however, systemd is the most widely used one. While the concepts are similar, knowing how modern Linux environments manage services is most useful for developers.
The basics
Section titled “The basics”Linux services are background processes that run on a Linux system to perform specific tasks. They are similar to Windows services or daemons on macOS.
Most non-containerized Linux environments use systemd to manage services. The two tools you’ll use to interact with systemd are:
systemctl: Controls services (called ‘units’ insystemdnomenclature)journalctl: Lets you work with system logs
We’ll cover systemctl in this chapter, and journalctl in Chapter 16, Monitoring Application Logs, later in the book.
systemd is a system and service manager for Linux that provides a standard way to manage services. It is now widely used as the default init system for most Linux distributions. Many Linux distributions previously made use of the SysV init systems, which come from Unix and are still used by many modern Unix operating systems. Others, such as Alpine and Gentoo Linux, use OpenRC as their init systems. There are many more init systems out there, however, the overwhelming majority of Linux distributions now use systemd. With systemd, services can be started, stopped, restarted, enabled (set to start at boot), and disabled, and their status can be checked. Services are defined by a unit file, which specifies exactly how the service should be managed by systemd.
To manage services with systemd, you can use the following basic commands (we’ll dive into each one later in this chapter):
systemctl start <service>: starts a service.systemctl stop <service>: stops a service.systemctl restart <service>: restarts a service.systemctl status <service>: displays the current status of a service.
Remember that only users with root privileges (e.g., using sudo) can manage system services with systemd.
Let’s take a quick detour to define a common term that you’ll see used often. In Linux, init – short for ‘initialization’ – is the first process that is started when the system boots up. Unsurprisingly, you can find it at PID 1. init is responsible for managing the boot process and starting all other processes and services that have been configured to run on the system. It also re-parents orphaned processes (processes whose original parent process has died) and keeps them as its own children, to ensure they still behave normally.
Like almost everything in the Linux world, there are several different, mutually exclusive programs that can fill this role. They are all referred to as init systems, which is the general name for any software that can fill this important bootstrapping, initializing, and coordinating role. As mentioned previously, there are several init systems available for Linux, including System V init (SysV), OpenRC, and systemd. Most modern Linux systems have switched to systemd, which is why that’s the one we’re covering here.
Which init system you’re using will determine how services are defined and managed, so keep in mind that everything you see here applies only to systemd.
Processes and services
Section titled “Processes and services”Let’s talk about the subtle difference between processes and services. You can think of a service as some packaging around a piece of software that makes it easier to manage as a running process.
A service adds convenient features to how a program (and the resulting process spawned by that program) is handled by the system. For example, it lets you define dependencies between different processes, control startup order, add environment variables for the process to start with, limit resource usage, control permissions, and many other useful things. To tie a bow around the whole package, a service provides a simple name to reference your program. We’ll show you how to create your own service in the later Chapter 10, Configuring Software.
In the rest of this chapter, we’ll stick to managing existing services.
systemctl commands
Section titled “systemctl commands”systemctl is the tool you’ll use to manage the services that have been defined on your system. These examples will use the foobar service, which doesn’t really exist, as a stand-in for whatever service you might be managing.
Checking the status of a service
Section titled “Checking the status of a service”systemctl status <service> checks the status of the service. You’ll get an assortment of data that’s useful for all kinds of troubleshooting tasks. This is what the output for the nginx web server’s service looks like:

Figure 3.1: nginx web server service output
Let’s dissect the information that’s displayed in the dense output that this command produces, line by line:
- Service name: The name of the service as defined in its unit file.
- Load state: Whether the service unit file has been successfully loaded and is ready to be started.
- Active state: The current state of the service – whether it is running, inactive, or failed – and how long it’s been that way.
- Docs: The main page where you can find relevant documentation if it’s been installed.
- Main PID and child processes: The process ID (PID) of the main process associated with the service, with additional entries for any child processes that have been launched.
- Resource usage: RAM (memory) and CPU time.
- CGroup: Details about the control group to which this process belongs.
- Log preview: A few loglines from the service’s output, to give you an idea of what’s happening.
This information provides a detailed overview of the service and its status and can be useful for debugging issues or checking the health of the service.
If the service has failed, the output will usually provide details on why it failed, such as the exit code or a description of the error.
Starting a service
Section titled “Starting a service”systemctl start foobarThis starts the service. If the service was already running, this command would have no effect.
Stopping a service
Section titled “Stopping a service”systemctl stop foobarThis stops the service. If the service wasn’t running, then it should have no effect.
Restarting a service
Section titled “Restarting a service”systemctl restart foobarThis stops and then starts a service. It is equivalent to running:
systemctl stop foobarsystemctl start foobarNote
Be careful with this command: if a service’s configuration file has changed on disk since it was started, and that config file has a bug that prevents the program from successfully starting, then restart will happily stop your running service and then fail to start it again.
This logical but potentially undesirable behavior has bitten many developers over the years, so take care to ensure that your service’s configuration is still valid before restarting.
Many popular programs have built-in configuration validation, e.g., for nginx, you can run:
nginx –tto test the configuration on disk.
Reloading a service
Section titled “Reloading a service”systemctl reload foobarNot all services support this subcommand – it’s up to the person creating the service configuration to implement it. If a service does have a reload option, it is generally safer than restart.
U sually, a reload:
- re-checks the configuration on disk to ensure that it’s valid
- re-reads the configuration into memory without interrupting the running process, if possible
- restarts the process only after validating the config and making sure the process will start successfully after being stopped
Like so many things in Linux, this is a convention rather than a strictly enforced requirement, so you may run into software that:
- doesn’t implement a
reloadsubcommand - doesn’t implement some of the safety features discussed above (config validation, etc.)
- does something else with a
reload, because the developer or packager thought it was a good idea
In general, when updating the configuration file for an application, especially in production environments, you should prefer reload over restart.
Enable and disable
Section titled “Enable and disable”systemctl enable foobar – configures foobar to start automatically on boot. systemctl disable foobar – if foobar is configured to start automatically, gets rid of that configuration and turns foobar into a manually managed service.
The key difference here is that while start and stop have an immediate effect – they ensure that a service is running (or stopped) right now; enable and disable are about future system startups. However, they have no effect on that service’s ‘running’ status at the time you run the command.
One common mistake that developers make is to assume that enable will start a service. It won’t. If you want to start an nginx web server now and ensure that it automatically starts every time the VM is rebooted, you need to run two commands:
systemctl start nginxsystemctl enable nginxBecause of this, enable and disable come with an optional flag that also starts (or stops, in the case of disable) the service. This command is equivalent to the two commands above:
systemctl enable --now nginxA note on Docker
Section titled “A note on Docker”While systemctl is a common tool for managing services on traditional Linux systems, it is generally not used in Docker containers due to containers’ isolated and self-contained nature.
Docker containers ideally run a single process and therefore don’t require a complex boot phase or process management. The container, in essence, is the process and doesn’t have access to the host system’s init system (including systemd).
Although it’s possible to have access to these commands in a Docker container, it’s usually undesirable to use any kind of service-management system inside of them.
Docker containers ideally contain a single application and launch a single process when they start. For this, no service management is needed – the running container is your service package, and your Docker container essentially is your process.
We don’t recommend a Docker setup that includes multiple processes or significant internal service management, so we won’t get into it here: much like families, all happy Docker images are alike in the same way, while each unhappy Docker configuration is unhappy in its own way.
Conclusion
Section titled “Conclusion”In this chapter, you learned how services are managed in Linux, and we introduced the practical commands you’ll use to control them. We gave you the theory you’ll need to make sense of all the terminology you’ll come across on a live system: what init is, what systemd does on Linux systems, and which commands you need to interact with it.
In the next chapter, we’ll show you some useful tricks for interacting with your shell and your command history, so you can save time and look like a Unix wizard from your favorite movie (it will also make you faster and more efficient at your day job, but putting it that way just doesn’t make it sound as fun).