If you’re like most system administrators, you probably use the systemctl command to manage your Linux services. But what do all those options mean? In this article, we’ll show you how to use systemctl to list your services, start, stop, and restart them. First things first: let’s take a look at the basic syntax for using systemctl. To list all of your installed services: systemctl list-services To see which services are currently running: systemctl status service_name To stop a service: systemctl stop service_name ..
Services and Daemons
Services and daemons are background tasks that run without a user interface, don’t require human interaction, and are usually started as the computer boots up.
At one time, services were launched by init, which was the very first process to be launched. The details of the services were held in a collection of scripts located in the “/etc/init/d” directory. On non-systemd distributions that’s still the case.
In the systemd world, services are launched by systemd which is the now first process to be launched. The details of the services are stored in unit files located in the “/usr/lib/systemd” directory.
According to its man page, systemd is a system and service manager. You can use the systemctl command to inspect and control different aspects of the systemd system, including services and daemons.
Because we’re looking at systemd-specific commands here, the first thing you need to know is whether you’re running a systemd-based distribution or not.
RELATED: Why Linux’s systemd Is Still Divisive After All These Years
init Or systemd Based?
The vast majority of Linux distributions use systemd, including Arch, Red Hat, and Debian, and many of the distributions derived from them. That includes the Ubuntu family of distributions, Fedora and its spins, and Manjaro and the other Arch-based distributions.
However, there are forks or flavors of some of these distributions that have been created specifically to avoid having to use systemd. Not only that, but there are other init systems that someone could choose to use instead of the one that came by default in their distribution, such as runit or s6-linux-init.
If you have to administer a Linux computer that you didn’t set up yourself, the only way to be certain if it is using systemd or not, is to check. We can do that by looking at the process tree with the pstree command. We only need to see the very top of the tree—we’re looking for the very first process that runs, after all—so we’ll pipe the output through the head command, and ask for the first five entries.
We can see that systemd is the first process that is run after boot, so we’re definitely on a systemd-based installation of Linux.
RELATED: How to Manage Processes from the Linux Terminal: 10 Commands You Need to Know
Using systemctl To List Services
The command to list services and daemons is systemctl. We can refine the systemctl command with the type and state options. We’re asking systemctl to report on services that are in the running state.
A table of information is generated. If it is too wide or long for your terminal window it is displayed in your default file viewer, which is likely going to be less.
To see the right-hand end of the table press the Right Arrow key. To return to the usual view, press the Left Arrow key.
Press the Q key to exit from less. The columns that are displayed are:
Unit: The name of the service or daemon. The column is titled “Unit” because whatever is in this column was launched using information systemd found in a unit file. Load: The load state of the service or daemon. It can be loaded, not-found, bad-setting, error, or masked. Active: The overall state the service or daemon is in. It can be active, reloading, inactive, failed, activating, or deactivating. SUB: The sub-state of the service or daemon. It can be dead, exited, failed, inactive, or running. Description: A short description of the unit.
We can pipe the output of systemctl through grep if we want to focus on a single service. This command isolates the table entry for the ssh service.
So far, we’ve been filtering the contents of the table by providing the state=running option. We can use any of the possible values of the sub-state instead: dead, exited, failed, inactive, or running.
Let’s look for failed services:
Combinations of sub-states can be used. Type them as a comma-separated list. Make sure you don’t include any whitespace between the options. Note that this finds services that match either state.
Pressing the Right Arrow key to look at the off-screen columns show that we have a mixture of exited and failed services in the list.
By default, systemctl lists processes—services and daemons—that have been launched by systemd because systemd found a unit file that contained a valid unit file for them. That’s why the shorthand term for all of these process is “units.”
There is an option to explicitly request systemctl to list units, but as it is the default action, it isn’t often used.
These commands produce the same results.
Using systemctl To List Unit Files
We can expand the scope of the systemctl command by including the list-unit-files option. This doesn’t just report on services and daemons that have been launched, it also lists all the unit files installed on your computer.
A colored table is displayed.
Removing the state option removes the filtering. The output will contain all installed unit files, regardless of their state.
The output will contain many more entries than the results from the previous commands.
On our test computer the results list is almost four times longer than the output of our previous commands.
If you do want to use the state option, you can use multiple states with it as we saw earlier. The same rules apply. Provide the options as comma separated values and don’t include any whitespace.
This command will list all unit files that are either disabled or failed to launch.
A reduced number of results is shown, filtered according to the selections you made with the state option.
Looking at One Service in Detail
If something about one service or daemon piques your interest and deserves a deeper dive, you can look at it in detail using the systemctl status option.
Let’s have a look at the SSH daemon, sshd. All we need to do is use the status option and the name of the service or daemon.
This compact display shows:
The name of the service together with a short description. A color-coded dot shows whether it is running or not. Green means it is running, red means it isn’t. What was loaded, including the path to the unit file. How long it has been running. Where the documentation is located in the man manual. The Process ID of the running instance. How many concurrent instances of this service are running. Usually this will be one. How much memory is being consumed. How much CPU time has been consumed. The control group the service belongs to.
Relevant entries from the system log are also shown. These are typically events such as the startup of the service. These can be informative if you’re looking into a service or daemon that didn’t launch correctly.
RELATED: How to Use journalctl to Read Linux System Logs
The Autonomic Systems
Services and daemons provide a lot of the automatic actions of your operating system, so they’re vital. That means their health is vital too.
Getting a view on your services, daemons, and unit files is easy, and informative. It’s also a valuable troubleshooting step if a service or daemon refuses to start.
RELATED: How to Solve the “Too Many Open Files” Error on Linux