Summary

Your Linux computer relies on a lot of background tasks called services or daemons. On systemd-based distributions you have built-in commands that let you see which services are running, disabled, or failed.

What Are 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 byinit, 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.

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

In the systemd world, services are launched bysystemdwhich 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,systemdis a system and service manager. you’re able to use thesystemctlcommand to inspect and control different aspects of the systemd system, including services and daemons.

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

Because we’re looking at systemd-specific commands here, the first thing you need to know is whether you’re running asystemd-baseddistribution or not.

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 theUbuntufamily of distributions,Fedoraand 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 asrunitors6-linux-init.

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

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 thepstreecommand. 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 theheadcommand, and ask for the first five entries.

We can see thatsystemdis the first process that is run after boot, so we’re definitely on a systemd-based installation of Linux.

Using grep to isolate a single service from the results

Using systemctl To List Services

The command to list services and daemons issystemctl. We can refine thesystemctlcommand with thetypeandstateoptions. We’re askingsystemctlto 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 beless.

Reporting on failed services with systemctl

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:

A mixture of failed and exited services found by systemctl

We can pipe the output ofsystemctlthroughgrepif we want to focus on a single service. This command isolates the table entry for thesshservice.

So far, we’ve been filtering the contents of the table by providing thestate=runningoption. We can use any of the possible values of the sub-state instead: dead, exited, failed, inactive, or running.

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

Let’s look for failed services:

Combinations of sub-states can be used. Type them as a comma-separated list. ensure 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.

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

By default,systemctllists processes—services and daemons—that have been launched bysystemdbecausesystemdfound 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 requestsystemctlto list units, but as it is the default action, it isn’t often used.

A mixture of disabled and failed unit files found by systemctl

Using systemctl To List Unit Files

We can expand the scope of thesystemctlcommand by including thelist-unit-filesoption. 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 thestateoption 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.

The details of a single service displayed by systemctl

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 thestateoption, 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’re able to 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:

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.

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.