How to List Linux Services With systemctl

Configurare noua (How To)

Situatie

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.

Solutie

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.

pstree | head -5

Using pstree piped through head to determine if a Linux installation is using systemd

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.

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.

systemctl --type=service --state=running

Using systemctl to list running services

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.

The output from a systemctl call displayed in the less file viewer

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.

The right-hand section of output from a systemctl call displayed in the less file viewer

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.

systemctl --type=service --state=running | grep ssh

Using grep to isolate a single service from the results

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:

systemctl --type=service --state=failed

Reporting on failed services with systemctl

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.

systemctl --type=service --state=failed,exited

Looking for services that have either failed or exited with systemctl

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.

A mixture of failed and exited services found by systemctl

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.

sudo systemctl list-units --type=service --state=running
sudo systemctl --type=service --state=running
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.

systemctl list-unit-files --state=enabled

Listing unit files with systemctl

A colored table is displayed.

A list of unit files generated by systemctl, displayed in the less file browser

Removing the state option removes the filtering. The output will contain all installed unit files, regardless of their state.

systemctl list-unit-files

Using systemctl to list unit files with no filtering

The output will contain many more entries than the results from the previous commands.

All the unit files listed by systemctl and displayed in the less file browser

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.

systemctl list-unit-files --state=enabled,failed

Using systemctl to look for unit files that match either of two states

A reduced number of results is shown, filtered according to the selections you made with the state option.

A mixture of disabled and failed unit files found by systemctl

Tip solutie

Permanent

Voteaza

(3 din 7 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?